Skip to content

vector_pro::push

C++
void push(const value_type& val);

Add element at the end

Adds a new element at the end of the vector, after its current last element. The content of val is copied to the new element.

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.

Same as vector::push_back()

Parameters

  1. val
    Value to be copied to the new element.

Return value

none

Example

C++
// vector_pro::push_back/push
#include <iostream>
#include "vector_pro.h"

/**
 * The example uses push to add a new element to the vector each time a new integer is read.
 */

int main ()
{
  vector_pro<int> myvector;
  int myint;

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

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

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

  return 0;
}

Complexity

Constant (amortized time, reallocation may happen).