C++ STL Vector

Declaration Syntax:

int n = 10, m = 20;    
vector<int> v = {7, 5, 16, 8};
vector<int> vec(10); //with initial size 10, vec[i] can be used to insert elements.
vector<int> vec2(10, 0); //intialized with zero.
vector<int> vec3(n);
vector<int> vec4(n, 0);
vector<vector<int>> grid(n, vector<int>(m)); // 2D vector of nxm
vector<vector<int>> grid2(n, vector<int>(m, 0)); //initialized with 0
vector<vector<int>> twoDVector;

Iterators

  1. begin() : returns an iterator pointing to the first element of the vector.
  2. end()  : returns an iterator pointing to the theoretical element that follows the last element of vector.
  3. rbegin() : returns a reverse iterator pointing to the last element of vector (reverse beginning). It moves from last to first element.
  4. rend() : returns a reverse iterator pointing to the the pseudo(fake) element that precedes first element of vector (considered as reverse end).
  5. cbegin() : returns a constant iterator pointing to the first element of vector.
  6. cend() : returns a constant iterator pointing to the pseudo(fake) element that follows the last element of vector.
  7. crbegin() : returns a constant reverse iterator pointing to the last element of vector (reverse beginning). It moves from last to first element
  8. crend() : returns a constant reverse iterator pointing to the pseudo(fake) element  that precedes the first element of vector (considered as reverse end)

Member Functions:

  1. size() : returns the number of elements of vector.
  2. max_size() : returns the maximum possible size the object of vector class can have. 
  3. capacity() : returns the size of the storage space currently allocated to the vector expressed as number of elements.
  4. resize() : resizes the container so that it contains n elements.
  5. empty() : returns whether the container is empty.
  6. shrink_to_fit() : reduces the capacity of the container to fit its size and destroys all elements beyond the capacity.
  7. reserve() : requests that the vector capacity be at least enough to contain n elements.

size() vs max_size() vs capacity():

 vector<int> vec;
vec.push_back(1);
cout <<"size: " << vec.size() << " max_size: " << vec.max_size() << " capacity: " << vec.capacity()<<endl;
vec.push_back(2);
cout << "size: " << vec.size() << " max_size: " << vec.max_size()<< " capacity: " << vec.capacity() << endl;
vec.push_back(3);
cout << "size: " << vec.size() << " max_size: " << vec.max_size()<< " capacity: " << vec.capacity() << endl;
vec.push_back(4);
cout << "size: " << vec.size() << " max_size: " << vec.max_size()<< " capacity: " << vec.capacity() << endl;
vec.push_back(5);
cout << "size: " << vec.size() << " max_size: " << vec.max_size()<< " capacity: " << vec.capacity() << endl;

 

Output:

size: 1 max_size: 4611686018427387903 capacity: 1

size: 2 max_size: 4611686018427387903 capacity: 2

size: 4 max_size: 4611686018427387903 capacity: 4

size: 3 max_size: 4611686018427387903 capacity: 4

size: 5 max_size: 4611686018427387903 capacity: 8


Element access:


  1. reference operator [index] : returns a reference to the element at position ‘index’ in the vector

  2. at(index) : returns a reference to the element at position ‘index’ in the vector

  3. front() : returns a reference to the first element in the vector.

  4. back() : returns a reference to the last element in the vector.

  5. data() : returns a direct pointer to the memory array used internally by the vector to store its owned elements.




Modifiers:


  1. assign() – It assigns new value to the vector elements by replacing old ones

  2. push_back() – It push the elements into a vector from the back

  3. pop_back() – It is used to pop or remove elements from a vector from the back.

  4. insert() – It inserts new elements before the element at the specified position

  5. erase() – It is used to remove elements from a container from the specified position or range.

  6. swap() – It is used to swap the contents of one vector with another vector of same type. Sizes may differ.

  7. clear() – It is used to remove all the elements of the vector container

  8. emplace() – It extends the container by inserting new element at position

  9. emplace_back() – It is used to insert a new element into the vector container, the new element is added to the end of the vector


Leave a comment