Is iterating through the vector using an iterator and copying to a list the most optimal method of copying. Any recommendations?
To convert a vector to list, R provides us with as. list() function that enables us to convert the single dimensional vector structure into a list format.
Use copy() Function to Convert a Vector to an Array The copy() method can be utilized to convert a vector to a double array so that data elements are copied to a different memory location.
Why would you iterate and not use the standard copy algorithm?
std::copy( vector.begin(), vector.end(), std::back_inserter( list ) );
If you're making a new list, you can take advantage of a constructor that takes begin and end iterators:
std::list<SomeType> myList(v.begin(), v.end());
Kasprzol's answer is perfect if you have an existing list you want to append to.
list.assign(vector.begin(), vector.end());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With