Skip to content
hengxin edited this page Feb 16, 2018 · 1 revision

Vector

Methods

- `void push_back( const T& value );`

The new element is initialized as a copy of value.

Requirement: `T` must meet the requirements of [`CopyInsertable`](http://en.cppreference.com/w/cpp/concept/CopyInsertable).
Yes, `std::vector<T>::push_back()` creates a copy of the argument and stores it in the vector. 
If you want to store pointers to objects in your vector, create a `std::vector<whatever*>` instead of `std::vector<whatever>`.

However, you need to make sure that the objects referenced by the pointers remain valid 
while the vector holds a reference to them (smart pointers utilizing the `RAII` idiom solve the problem).

Use emplace\_back to transfer ownship of objects by moving them into containers.

This post compares Java with C++ on this isssue regarding the performance.

- `void push_back( T&& value );` (since C++11)

value is moved into the new element.

Requirement: `T` must meet the requirements of [`MoveInsertable`](http://en.cppreference.com/w/cpp/concept/MoveInsertable).
- void emplace\_back( Args&&... args ); 

Applications

Braces Balance

Operating Systems

  • call stack

Compiler Theory

  • expression evaluation
Clone this wiki locally