C++ Program to Convert Vector to a List


Vectors in C++ are dynamic arrays that can contain any type of data, it can be user-defined or primitive. Dynamic is in the sense that the size of a vector can increase or decrease according to the operations. Vectors have support for various functions, for which data manipulation is very easy. Lists on the other hand are containers same as vectors, but list implementation is based on doubly linked lists compared to the array implementation of vectors. Lists offer the same constant time operations anywhere in it, that is the main feature of using lists. We take a look at the major ways to convert a vector into a list.

Using Range Constructor

To use the range constructor, the beginning and the ending pointer of the vector has to be passed as an argument to the constructor while creating the list.

Syntax

vector <int> ip;
list <int> op( ip.begin(), ip.end() );

Algorithm

  • Take input in a vector.
  • Pass the beginning and the ending pointer of the vector to the range constructor of the list.
  • Display the contents of the list.

Example

#include <iostream> #include <vector> #include <list> using namespace std; list <int> solve( vector <int> ip) { //initialise the list list <int> op( ip.begin(), ip.end() ); return op; } int main() { vector <int> ip( { 15, 20, 65, 30, 24, 33, 12, 29, 36, 58, 96, 88, 30, 71 } ); list <int> op = solve( ip ); //display the input cout<< "The input vector is: "; for( int i : ip ) { cout<< i << " "; } //display the output cout << "\nThe output list is: "; for( int j : op ) { cout << j << " "; } return 0; }

Output

The input vector is: 15 20 65 30 24 33 12 29 36 58 96 88 30 71 
The output list is: 15 20 65 30 24 33 12 29 36 58 96 88 30 71

Using the assign function of std::list

The usage of std::list is similar to the usage of the range constructor. We pass the beginning and end pointer of the vector the same way as we passed in the range constructor.

Syntax

vector <int> ip;
list <int> op();
op.assign(ip.begin(), ip.end());

Algorithm

  • Take input in a vector.
  • Define a new list.
  • Pass the beginning and the ending pointer of the vector to the assign function of the list
  • Display the contents of the list.

Example

#include <iostream> #include <vector> #include <list> using namespace std; list <int> solve( vector <int> ip) { //initialise the list list <int> op; op.assign( ip.begin(), ip.end() ); return op; } int main() { vector <int> ip( { 40, 77, 8, 65, 92 ,13, 72, 30, 67, 12, 88, 37, 18, 23, 41} ); list <int> op = solve( ip ); //display the input cout<< "The input vector is: "; for( int i : ip ) { cout<< i << " "; } //display the output cout << "\nThe output list is: "; for( int j : op ) { cout << j << " "; } return 0; }

Output

The input vector is: 40 77 8 65 92 13 72 30 67 12 88 37 18 23 41 
The output list is: 40 77 8 65 92 13 72 30 67 12 88 37 18 23 41 

Using the list insert function

We can insert data into a list from a vector using the insert function of the list. The list takes the beginning of the list pointer and the pointers of the beginning and the ending of the vector.

Syntax

vector <int> ip;
list <int> op();
op.insert(op.begin(), ip.begin(), ip.end());

Algorithm

  • Take input in a vector.
  • Define a new list.
  • Pass the beginning pointer of the list and the beginning and the ending pointer of the vector to the insert function of the list.
  • Display the contents of the list.

Example

#include <iostream> #include <vector> #include <list> using namespace std; list <int> solve( vector <int> ip) { //initialise the list list <int> op; op.insert( op.begin(), ip.begin(), ip.end() ); return op; } int main() { vector <int> ip( { 30, 82, 7, 13, 69, 53, 70, 19, 73, 46, 26, 11, 37, 83} ); list <int> op = solve( ip ); //display the input cout<< "The input vector is: "; for( int i : ip ) { cout<< i << " "; } //display the output cout << "\nThe output list is: "; for( int j : op ) { cout << j << " "; } return 0; }

Output

The input vector is: 30 82 7 13 69 53 70 19 73 46 26 11 37 83 
The output list is: 30 82 7 13 69 53 70 19 73 46 26 11 37 83 

Conclusion

Conversion from a vector to a list in C++ has the benefits of uniform operation complexities at any position in the list. There are a few more methods using which we can convert a vector to a list. However, we have only mentioned the simplest and fastest methods here.

Updated on: 19-Oct-2022

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements