Convert a vector to a list in C++
This post will discuss how to convert a vector to a list in C++.
1. Using Range Constructor
The idea is to pass two input iterators pointing to the beginning and end of the given vector to the range constructor of the list class. This is a very effective, simple, short, and clear approach.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> #include <vector> #include <list> int main() { // input vector of integers std::vector<int> src({ 1, 2, 3, 4, 5 }); std::list<int> dest(src.begin(), src.end()); for (const int &i: dest) { std::cout << i << " "; } return 0; } |
Output:
1 2 3 4 5
2. Using std::copy function
If we need to copy elements of a vector to an existing list, the efficient solution is to use the standard algorithm std::copy, which inserts the elements of a source sequence into a destination container with the output iterator.
The output iterator is usually the iterator to the initial position in the destination container. The destination container should have sufficient space to accommodate elements of the source sequence. However, if this is not the case, the workaround uses insert iterators like std::back_inserter, which internally calls the push_back function on the destination container.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> #include <vector> #include <list> int main() { // input vector of integers std::vector<int> src({ 1, 2, 3, 4, 5 }); std::list<int> dest; std::copy(src.begin(), src.end(), std::back_inserter(dest)); for (const int &i: dest) { std::cout << i << " "; } return 0; } |
Output:
1 2 3 4 5
3. Using std::list::insert function
We can also use the insert() function of the list class, which takes a starting position in the list for inserting the new elements and two input iterators pointing to the range of elements in the source sequence.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> #include <vector> #include <list> int main() { // input vector of integers std::vector<int> src({ 1, 2, 3, 4, 5 }); std::list<int> dest; dest.insert(dest.begin(), src.begin(), src.end()); for (const int &i: dest) { std::cout << i << " "; } return 0; } |
Output:
1 2 3 4 5
4. Using std::list::assign function
We can also overwrite existing elements in the list container with new elements in the specified range with the help of the assign() function of the list class.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> #include <vector> #include <list> int main() { // input vector of integers std::vector<int> src({ 1, 2, 3, 4, 5 }); std::list<int> dest; dest.assign(src.begin(), src.end()); for (const int &i: dest) { std::cout << i << " "; } return 0; } |
Output:
1 2 3 4 5
5. Naive Solution
Finally, we can even write our own routine for converting vector to a list. We create an empty list, traverse the vector using for-loop, and insert every element into the list. Starting C++11, the range-based for-loop, is the most elegant way to traverse any container.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <iostream> #include <vector> #include <list> int main() { // input vector of integers std::vector<int> src({ 1, 2, 3, 4, 5 }); std::list<int> dest; for (const int &i: src) { dest.push_back(i); } for (const int &i: dest) { std::cout << i << " "; } return 0; } |
Output:
1 2 3 4 5
That’s all about converting a vector to a list in C++.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)