cppreference.com -> C++ Vectors -> Details

C++ Vectors


Constructors

Syntax:
  vector();
  vector( size_type num, const TYPE &val );
  vector( const vector &from );
  vector( input_iterator start, input_iterator end );

C++ Vectors can be constructed with either:

For example, the following example constructs a vector consisting of five copies of the integer 42.
vector<int> v1( 5, 42 );
 

Operators

Syntax:
  v1 == v2
  v1 != v2
  v1 <= v2
  v1 >= v2
  v1 < v2
  v1 > v2 
  v[]

The C++ Vectors can be compared using the standard operators: ==, !=, <=, >=, <, and >. Individual elements of a vector can be examined with the [] operator.

Two vectors are equal if

  1. their size is the same, and
  2. each member in location i in one vector is equal to the the member in location i in the other vector.
Comparisons among vectors are done lexicographically.

Related topics:
at().

assign

Syntax:
  void assign( input_iterator start, input_iterator end );
  void assign( size_type num, const TYPE &val );

The assign() function either gives the current vector the values from start to end, or gives it num copies of val. This function will destroy the previous contents of the vector.


at

Syntax:
  TYPE at( size_type loc );

The at() function returns a reference to the element in the current vector at loc. The at() function is safer than the [] operator, because it won't let you reference items outside the bounds of the vector. For example, consider the following code:

vector<int> v( 5, 1 );
for( int i = 0; i < 10; i++ ) {
  cout << "Element " << i << " is " << v[i] << endl;
}
 

This code overrunns the end of the vector, producing potentially dangerous results. The following code would be much safer:

vector<int> v( 5, 1 );
for( int i = 0; i < 10; i++ ) {
  cout << "Element " << i << " is " << v.at(i) << endl;
}
 

Instead of attempting to read garbage values from memory, the at() function will realize that it is about to overrun the vector and will throw an exception.

Related topics:
The [] Operator

back

Syntax:
  TYPE back();

The back() function returns a reference to the last element in the current vector. For example:

vector<int> v;
for( int i = 0; i < 5; i++ ) {
  v.push_back(i);
}
cout << "The first element is " << v.front() 
     << " and the last element is " << v.back() << endl;
 

This code produces the following output:

The first element is 0 and the last element is 4
 
Related topics:
front().

begin

Syntax:
  iterator begin();

The function begin() returns an iterator to the first element in the current vector. For example, the following code uses an iterator to display all of the elements in a vector:

vector<int> v1( 5, 789 );
vector<int>::iterator it;
for( it = v1.begin(); it != v1.end(); it++ )
  cout << *it << endl;
Related topics:
end().

capacity

Syntax:
  size_type capacity();

The capacity() function returns the number of elements that the current vector can hold before it will need to allocate more space.


clear

Syntax:
  void clear();

The function clear() deletes all of the elements in the current vector.


empty

Syntax:
  bool empty();

The empty() function returns true if the current vector has no elements, false otherwise. For example, the following code clears a given vector and displays its contents in reverse order:

vector<int> v;
for( int i = 0; i < 5; i++ ) {
  v.push_back(i);
}
while( !v.empty() ) {
  cout << v.back() << endl;
  v.pop_back();
}
 
Related topics:
size()

end

Syntax:
  iterator end();

The end() function returns an iterator just past the end of the current vector. Note that before you can access the last element of the vector, you'll have to decrement the iterator.

Related topics:
begin()

erase

Syntax:
  iterator erase( iterator loc );
  iterator erase( iterator start, iterator end );

The erase() function either deletes the element at location loc, or deletes the elements between start and end. The return value is the element after the last element erased. For example:

// Create a vector, load it with the first ten characters of the alphabet
vector<char> alphaVector;
for( int i=0; i < 10; i++ )
  alphaVector.push_back( i + 65 );
int size = alphaVector.size();
vector<char>::iterator startIterator;
vector<char>::iterator tempIterator;
for( int i=0; i < size; i++ ) {
  startIterator = alphaVector.begin();
  alphaVector.erase( startIterator );
  // Display the vector
  for( tempIterator = alphaVector.begin(); tempIterator != alphaVector.end(); tempIterator++ )
    cout << *tempIterator;
  cout << endl;
}

That code would display the following output:

BCDEFGHIJ
CDEFGHIJ
DEFGHIJ
EFGHIJ
FGHIJ
GHIJ
HIJ
IJ
J
Related topics:
pop_back().

front

Syntax:
  TYPE front();

The front() function returns a reference to the first element of the current vector.

Related topics:
back().

get_allocator

Syntax:
  allocator_type get_allocator();

The function get_allocator() returns the current vector's allocator.


insert

Syntax:
  iterator insert( iterator loc, const TYPE &val );
  void insert( iterator loc, size_type num, const TYPE &val );
  void insert( iterator loc, input_iterator start, input_iterator end );

The function insert() either:

For example:
// Create a vector, load it with the first 10 characters of the alphabet
vector<char> alphaVector;
for( int i=0; i < 10; i++ )
  alphaVector.push_back( i + 65 );
// Insert four C's into the vector
vector<char>::iterator theIterator = alphaVector.begin();
alphaVector.insert( theIterator, 4, 'C' );
// Display the vector
for( theIterator = alphaVector.begin(); theIterator != alphaVector.end(); theIterator++ )
  cout << *theIterator;

This code would display:

CCCCABCDEFGHIJ

max_size

Syntax:
  size_type max_size();

The max_size() function returns the maximum number of elements that the current vector can hold.


pop_back

Syntax:
  void pop_back();

The function pop_back() deletes the last element in the current vector. For example:

vector<char> alphaVector;
for( int i=0; i < 10; i++ )
  alphaVector.push_back( i + 65 );
int size = alphaVector.size();
vector<char>::iterator theIterator;
for( int i=0; i < size; i++ ) {
  alphaVector.pop_back();
  for( theIterator = alphaVector.begin(); theIterator != alphaVector.end(); theIterator++ )
    cout << *theIterator;
  cout << endl;
}

This code displays the following output:

ABCDEFGHI
ABCDEFGH
ABCDEFG
ABCDEF
ABCDE
ABCD
ABC
AB
A
Related topics:
erase().

push_back

Syntax:
  void push_back( const TYPE &val );

The push_back() function appends val to the end of the current vector.


rbegin

Syntax:
  reverse_iterator rbegin();

The rbegin() function returns a reverse iterator to the end of the current vector.


rend

Syntax:
  reverse_iterator rend();

The function rend() returns a reverse iterator to the beginning of the current vector.


reserve

Syntax:
  void reserve( size_type size );

The reserve() function sets the capacity of the current vector to at least size.


resize

Syntax:
  void resize( size_type size, TYPE val );

The function resize() changes the size of the current vector to size, setting any newly-created elements to val.


size

Syntax:
  size_type size();

The size() function returns the number of elements in the current vector.

Related topics:
empty()

swap

Syntax:
  void swap( vector &from );

The swap() function exchanges the elements of the current vector with those of from.