Vectors in C are dynamic arrays and can contain any type of data, either user-defined or primitive. Dynamic means that the size of the vector can increase or decrease based on operations. Vectors support various functions and data manipulation is very easy. On the other hand, a list is the same container as a vector, but compared to the array implementation of vectors, the list implementation is based on a doubly linked list. Lists provide the same constant-time operation everywhere in them, which is the main feature of using lists. Let's look at the main methods of converting vectors to lists.
To use the range constructor, you must pass the start and end pointers of the vector as parameters to the constructor when creating the list.
vector <int> ip; list <int> op( ip.begin(), ip.end() );
#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; }
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
The usage of std::list is similar to that of the range constructor. We pass the start and end pointers of the vector in the same way as the range constructor.
vector <int> ip; list <int> op(); op.assign(ip.begin(), ip.end());
#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; }
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
We can use the insert function of the list to insert data from the vector into the list. Lists require pointers to the start of the list and pointers to the start and end of the vector.
vector <int> ip; list <int> op(); op.insert(op.begin(), ip.begin(), ip.end());
#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; }
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
In C, converting a vector to a list has the benefit of uniform operation complexity anywhere in the list. There are several ways to convert a vector into a list. However, we have only mentioned the simplest and fastest methods here.
The above is the detailed content of C++ program to convert vector to list. For more information, please follow other related articles on the PHP Chinese website!