Section Navigation

Reference: Vector Class

The vector class is part of the Standard Template Library and accepts all data types. Vectors are defined by

vector< T > vec;

where T is the basic data type (e.g. int, double). The vector class is included with #include <vector> and belongs to the standard namespace. Single elements can be access as those of the C array type (e.g. vec[4]).

As one of the STL container classes, the vector class supports iterater, which is a more generic approach then the index for arrays.

Access Member Functions

Init with n objects, using default constructor for T vector<double> vec(20);
Init with n objects of a given value vector<double> vec(20,3.14);
Init with another vector of same type vector<double> vec(vec2);
Current size vec.size();
Capacity vec.capacity();
Cnage capacity vec.reserve(n);
Check if empty, returns bool vec.empty();
Maximum number of allowed elements vec.max_size();
Resizing vec.resize(n);
Removing all elements vec.clear();
Remove one element at position i, returns iterator vec.erase(i);
Remove elements between position i and j, returns iterator vec.erase(i,j);
Insert element before position i vec.insert(i,3.14);
Append element at end vec.push_back(3.14);
Remove last element vec.pop_front();
Position of first element, returns iterator vec.begin();
Position of last element, returns iterator vec.end();
Iterator vector< T >::iterator i;
Reverse iterator vector< T >::reverse_iterator r;
Position of first element, returns reverse iterator vec.rbegin();
Position of last element, return reverse iterator vec.rend();