Revision as of 09:02, 6 July 2020 by Cubbi (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


The vector container, declared in <vector>, allows for keeping a variable sequence of elements of arbitrary data type. These are its essential properties:

  • The data type of the elements in the vector can be defined as a template argument:
std::vector<int> v1; // a vector of integers
std::vector<int*> v2; // a vector of pointers to integer(s)
std::vector<std::string> v3; // a vector of strings
std::vector<std::vector<int> > v3; // a vector of vectors of int's
  • Vectors have a variable number of elements, which can be inserted, removed and accessed in runtime:
using namespace std;
vector<int> vec = {1,2,3}; // you can initialize vectors with an initializer list since C++11
cout << vec.size() << endl; // will print 3 
vec.push_back(5); // insert 5 at the end of the vector
cout << vec[3] << endl; // will print 5 
vec[1] = 10; // change the second element to 10
cout << vec.front() << endl; // first element, will print 1 
cout << vec.size() << endl; // will print 4
  • Elements in a vector are contiguous like in an array, so it provides fast read/write operations on any element. Internally, these are kept in a dynamically allocated array of larger (or equal) size, so that it can grow larger. If this array is full before inserting another element, the vector will relocate every single element currently in it into a larger array. This means two things: there is no theoretical limit on the number of elements of a vector, but inserting many elements at a time will force a capacity increase of the vector, which can become a heavy operation. You can use the capacity() member function in order to find to what size can the vector grow to without an expansion.
vector<int> vec = {0, 3, 4, 6};
cout << vec.capacity() << endl; // will print a number greater or equal to 4
vec.reserve(10); // you can also force an expansion of the vector
cout << vec.capacity() << endl; // will print 10
vec.shrink_to_fit(); // since C++11, you can contract the vector to the minimum capacity needed
cout << vec.capacity() << endl; // will print 4
  • Access to an element out of the vector's range (using a bad index for example) will not necessarily cause an error with the array suffix operator[]. On the other hand, the at() function includes bounds checking.
vector<float> vec (3, 2.0f); // initialize a vector with 3 elements set to 2.0f
float myfloat1 = vec[2]; // OK
float myfloat2 = vec[3]; // undefined behavior!
vec[6] = myfloat1; // undefined behavior!
float myfloat3 =; // OK = 1.0f; // will throw std::out_of_range exception!
  • Like any other container, they can be iterated using a standard iterator or a for-each statement:
vector<int> vec = {5, 3, 1};
for (auto it = begin(vec) ; it != end(vec) ; it++) {
 cout << *it << endl; // print each element
for (int& e : vec) {
 e++; // increment each element by one

Vectors are preferred when keeping a sequence of elements for random access due to its O(1) complexity. Once created, it can be accessed in a similar fashion to an array. If the needed capacity for the vector is unknown, it can still be used without major issues, thanks to the various element insertion and removal functions available.