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.
Insertion: Insertion in array of vectors is done using push_back() function. Above pseudo-code inserts element 35 at every index of vector <int> A[n]. Traversal: Traversal in an array of vectors is perform using iterators.
To add elements to vector, you can use push_back() function. push_back() function adds the element at the end of this vector. Thus, it increases the size of vector by one.
Appending to a vector means adding one or more elements at the back of the vector. The C++ vector has member functions. The member functions that can be used for appending are: push_back(), insert() and emplace(). The official function to be used to append is push_back().
I'm currently using C++17, MinGW64. 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:
STL Vector class provides the insert () function to insert elements in a vector at a specific index. In this example, we are going to use the insert () function to insert an element in a vector.
The insert method can be utilized even if the vector elements are allocated manually using new call; The insert method can also be applied when two vectors of strings need to be concatenated. The following example demonstrates the given case with almost the same syntax as used with the integer vectors.
In this article, we have explored Different ways to append two vector in C++ STL which include std::copy, insert, concat, overloaded + operator and other functions. 1. STL vs Standard Library STL stands from Standard Template Library and as the name suggests it is a library made of generic classes. The syntax to declare one of it is:
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.
Live
"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).
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