Convert a list to an array in C++

In this post, we will discuss how to convert a list to an array in C++.


 

Simple solution would be to use range-based for loop to traverse the list and one by one add each element at next available index in the array.

 

Download   Run Code

Output:

1 2 3 4 5

 
We can also use STL algorithm std::copy to insert elements of a container into another container as shown below. The destination container needs to have enough space to accommodate all elements.

 

Download   Run Code

Output:

1 2 3 4 5

 
Thanks for reading.

Please use ideone or C++ Shell or any other online compiler link to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂
 


Get great deals at Amazon




Categories

Leave a Reply

avatar
  Subscribe  
Notify of