Convert an array to a list in C++
This post will discuss how to convert an array to a list in C++.
1. Using Range Constructor
A simple solution is to use the range constructor of std::list
, which constructs a new list initialized by elements of the specified range (defined by two input iterators).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> #include <list> int main() { int arr[] = { 10, 15, 20, 25 }; std::list<int> ints(std::begin(arr), std::end(arr)); // or, use // std::list<int> ints(arr, arr + sizeof(arr)/sizeof(int)); for (int i: ints) { std::cout << i << " "; } return 0; } |
Output:
10 15 20 25
2. Using std::insert
function
For an existing list, we can use the overloaded std::insert
function that takes three parameters. The first parameter is an iterator to the destination list. The second and third parameters are the iterators specifying a range of the array elements.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <iostream> #include <list> int main() { int arr[] = { 10, 15, 20, 25 }; std::list<int> ints; ints.insert(ints.begin(), std::begin(arr), std::end(arr)); // or, use // ints.insert(ints.begin(), arr, arr + sizeof(arr)/sizeof(int)); for (int i: ints) { std::cout << i << " "; } return 0; } |
Output:
10 15 20 25
3. Using range-based for-loop
We can also use the range-based for-loop that loops through the array elements and adds each encountered element to the list using list::push_back
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> #include <list> int main() { int arr[] = { 10, 15, 20, 25 }; std::list<int> ints; for (int i: arr) { ints.push_back(i); } for (int i: ints) { std::cout << i << " "; } return 0; } |
Output:
10 15 20 25
4. Using std::copy
function
Another solution is to use the std::copy
, which copies the specified range of elements to another range beginning from the specified output iterator.
We can either use std::back_inserter
to the initial position in the destination list that calls the push_back()
function behind the scenes. If the list has enough storage, we can simply specify the output iterator to the beginning of the destination range.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include <iostream> #include <list> #include <iterator> #include <algorithm> int main() { int arr[] = { 10, 15, 20, 25 }; std::list<int> ints; std::copy(std::begin(arr), std::end(arr), std::back_inserter(ints)); // or, use // int n = sizeof(arr) / sizeof(arr[0]); // std::list<int> ints(n); // std::copy(arr, arr + n, ints.begin()); for (int i: ints) { std::cout << i << " "; } return 0; } |
Output:
10 15 20 25
5. Using std::assign
function
We can also use the std::assign
function that replaces the existing contents of the list with elements of the specified range.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <iostream> #include <list> int main() { int arr[] = { 10, 15, 20, 25 }; std::list<int> ints; ints.assign(std::begin(arr), std::end(arr)); // or, use // int n = sizeof(arr)/sizeof(int); // ints.assign(arr, arr + n); for (int i: ints) { std::cout << i << " "; } return 0; } |
Output:
10 15 20 25
That’s all about converting an array 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 :)