push_back is a C++ sequence-container member function that appends an already constructed element to the container. With std::vector, it adds the value at the end. section 2.2.2
Use push_back when:
- You already have an object to add, such as a calculated row. section 2.2.2
- You want to add an element at the container’s end; use insert for another position. section 2.2.2
- You need to choose between copying an lvalue and moving a temporary through overload resolution. section 2.2.3
Building Pascal’s triangle
A generated row can be appended with triangle.push_back(data). If get_next_row(...) produces a temporary, triangle.push_back(get_next_row(triangle.back())) can use the rvalue overload and avoid copying the row. section 2.2.2 section 2.2.3
Listing 2.4 Moving a temporary
auto generate_triangle(int rows)
{
std::vector<std::vector<int>> triangle{ {1} }; #1
for (int row = 1; row < rows; ++row) #2
{
triangle.push_back(get_next_row(triangle.back()));
}
return triangle;
}
Inspect the complete Pascal’s-triangle example, including push_back with a temporary row and the loop that generates successive rows. section 2.2.3

