Convert a list to a vector in C++
This post will discuss how to convert a list to a vector in C++.
1. Using range-based for-loop
A naive solution is to use a range-based for-loop (or simple for-loop) to iterate the list and individually add each encountered element to an empty vector.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> #include <vector> #include <list> int main() { std::list<char> list = { 'a', 'b', 'c' }; std::vector<char> result; for (char const &c: list) { result.push_back(c); } for (char const &c: result) { std::cout << c << ' '; } return 0; } |
Output:
a b c
Please note that the push_back function is used to preserve the order of elements present in the list.
2. Using Range Constructor
We can also use the range constructor offered by the vector class that takes two input iterators pointing to the beginning and the end of an input sequence.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> #include <list> #include <vector> #include <algorithm> int main() { std::list<char> list = { 'a', 'b', 'c' }; std::vector<char> result(list.begin(), list.end()); for (char const &c: result) { std::cout << c << ' '; } return 0; } |
Output:
a b c
We can also use a move iterator to “move” all elements from the list to a vector.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> #include <list> #include <vector> #include <algorithm> int main() { std::list<char> list = { 'a', 'b', 'c' }; std::vector<char> result{ std::make_move_iterator(list.begin()), std::make_move_iterator(list.end()) }; for (char const &c: result) { std::cout << c << ' '; } return 0; } |
Output:
a b c
3. Using std::copy function
We can use the standard algorithm std::copy to insert elements of a container into another container, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> #include <list> #include <vector> #include <algorithm> int main() { std::list<char> list = { 'a', 'b', 'c' }; std::vector<char> result(list.size()); std::copy(list.begin(), list.end(), result.begin()); for (char const &c: result) { std::cout << c << ' '; } return 0; } |
Output:
a b c
As evident from the above code, the destination container needs enough space to accommodate all elements. If enough space is not available, the workaround uses insert iterators such as std::back_inserter.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> #include <list> #include <vector> #include <algorithm> int main() { std::list<char> list = { 'a', 'b', 'c' }; std::vector<char> result; std::copy(list.begin(), list.end(), std::back_inserter(result)); for (char const &c: result) { std::cout << c << ' '; } return 0; } |
Output:
a b c
That’s all about converting a list to a vector 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 :)