Reputation: 688
I'm relatively new to c++ and I've tried to do some research, but while searching online I've mainly come across C arrays rather than std::array
. What are the most efficient ways to append std::array elements into a std::vector, and to insert std::array
elements into a std::vector
? Should I use STL functions such as std::copy
? I'm currently using C++17, MinGW64.
Upvotes: 6
Views: 3673
Reputation: 4263
To append the elements of an existing array (or other range in general) to a vector you can just use the vector's insert overload for an iterator range:
vector<int> vec{1, 2, 3};
array<int, 3> arr{4, 5, 6};
// arr could be some other container or bare array as well, for ex.:
// int arr[] = {4, 5, 6};
// vector<int> arr {4, 5, 6};
// list<int> arr {4, 5, 6};
// ...
vec.insert(vec.end(), begin(arr), end(arr)); // insert at vec.end() = append
//or vec.insert(vec.end(), arr.begin(), arr.end()); // insert at vec.end() = append
Note that if you have some other type instead of int
which is expensive to copy, and you want to move the elements from the source array, you can use
move_iterator
, for ex.
vec.insert(vec.end(), move_iterator(arr.begin()), move_iterator(arr.end()));
For operations on ranges in general, the container member functions are to be preferred instead of the same-named functions from the <algorithm>
header.
So for example in this case the vec.insert
will insert the range at once, as opposed if have had used the std::insert
where the elements would be inserted one by one.
This is very nice explained in the Scott Meyers Effective STL.
Upvotes: 11
Reputation: 38315
"Appending" values can mean two things - you either want to copy/duplicate the elements in question, or you don't need them in the source container aftwards and might also move them into the destination container. Also, it makes sense to distinguish between insertion at construction time vs. appending to an existing, already constructed container: if you can, always construct a container with the elements that it's supposed to own.
Copy-append std::array
elements to an already constructed std::vector
:
std::vector<T> dest;
std::array<T, N> source;
// ...
dest.insert(dest.end(), source.cbegin(), source.cend());
Move-append std::array
elements to an already constructed std::vector
.
std::vector<T> dest;
std::array<T, N> source;
// ...
dest.insert(dest.end(), std::move_iterator(source.begin()),
std::move_iterator(source.end()));
Copy std::array
elements into a std::vector
at construction:
std::array<T, N> source;
// ...
std::vector<T> dest(source.cbegin(), source.cend());
Move std::array
elements into a std::vector
at construction:
std::array<T, N> source;
// ...
std::vector<T> dest(std::move_iterator(source.begin()),
std::move_iterator(source.cend()));
There is not much to add here when talking about insertion into the middle - the only notable difference is that it will always be less efficient, as the remaining elements in the destination std::vector
will be move-constructed (which is O(N)).
Note also that for appending elements, there is std::move
and std::copy
from the <algorithm>
header (where std::copy
can be used with std::move_iterator
s). However, these cannot be as efficient as a direct call to std::vector::insert
, because the former operates on the iterator abstraction and processes the copy/move one element at a time without knowing about the storage details of the destination (this can result in multiple buffer resizings), while the latter is a std::vector
member function and will resize the buffer only once (if required).
Upvotes: 10