public member function
std::vector::emplace_back
<vector>
template <class... Args>
void emplace_back (Args&&... args);
Construct and insert element at the end
Inserts a new element at the end of the vector, right after its current last element. This new element is constructed in place using args as the arguments for its constructor.
This effectively increases the container size by one, which causes an automatic reallocation of the allocated storage space if -and only if- the new vector size surpasses the current vector capacity.
A similar member function exists, push_back, which either copies or moves an existing object into the container.
Parameters
- args
- Arguments passed to the constructor of the new element.
Return value
none.
If a reallocation happens, the storage is allocated using the container's allocator, which may throw exceptions on failure (for the default allocator, bad_alloc is thrown if the allocation request does not succeed).
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
// vector::emplace_from
#include <iostream>
#include <vector>
int main ()
{
std::vector<int> myvector = {10,20,30};
myvector.emplace_back (100);
myvector.emplace_back (200);
std::cout << "myvector contains:";
for (auto& x: myvector)
std::cout << ' ' << x;
std::cout << '\n';
return 0;
}
|
Output:
myvector contains: 10 20 30 100 200
|
Complexity
Constant (amortized time, reallocation may happen).
If a reallocation happens, the reallocation is itself up to linear in the entire size.
Iterator validity
If a reallocation happens, all iterators, pointers and references related to this container are invalidated.
Otherwise, only the end iterator is invalidated, and all other iterators, pointers and references to elements are guaranteed to keep referring to the same elements they were referring to before the call.
Data races
The container is modified.
If a reallocation happens, all contained elements are modified.
Otherwise, no existing element is accessed, and concurrently accessing or modifying them is safe.
See also
vector::emplace_frontvector::insert_back- vector::emplace
- Construct and insert element (public member function
)
- vector::erase
- Erase elements (public member function
)
- vector::assign
- Assign vector content (public member function
)