Aug 29, 2018

C++ Basics

C++

std::vector::push_back()

void push_back (const value_type& val);
void push_back (value_type&& val);

Adds a new element at the end of the vectorarrow-up-right, after its current last element. The content of val is copied (or moved) to the new element. This effectively increases the container sizearrow-up-right by one, which causes an automatic reallocation of the allocated storage space if -and only if- the new vector sizearrow-up-right surpasses the current vector capacityarrow-up-right.

// vector::push_back
#include <iostream>
#include <vector>

int main ()
{
  std::vector<int> myvector;
  int myint;

  std::cout << "Please enter some integers (enter 0 to end):\n";

  do {
    std::cin >> myint;
    myvector.push_back(myint);
  } while (myint);

  std::cout << "myvector stores " << int(myvector.size()) << " numbers.\n";

  return 0;
}

Last updated