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
- begin() : returns an iterator pointing to the first element of the vector.
- end() : returns an iterator pointing to the theoretical element that follows the last element of vector.
- rbegin() : returns a reverse iterator pointing to the last element of vector (reverse beginning). It moves from last to first element.
- rend() : returns a reverse iterator pointing to the the pseudo(fake) element that precedes first element of vector (considered as reverse end).
- cbegin() : returns a constant iterator pointing to the first element of vector.
- cend() : returns a constant iterator pointing to the pseudo(fake) element that follows the last element of vector.
- crbegin() : returns a constant reverse iterator pointing to the last element of vector (reverse beginning). It moves from last to first element
- 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:
- size() : returns the number of elements of vector.
- max_size() : returns the maximum possible size the object of vector class can have.
- capacity() : returns the size of the storage space currently allocated to the vector expressed as number of elements.
- resize() : resizes the container so that it contains n elements.
- empty() : returns whether the container is empty.
- shrink_to_fit() : reduces the capacity of the container to fit its size and destroys all elements beyond the capacity.
- 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: 1size: 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:
- reference operator [index] : returns a reference to the element at position ‘index’ in the vector
- at(index) : returns a reference to the element at position ‘index’ in the vector
- front() : returns a reference to the first element in the vector.
- back() : returns a reference to the last element in the vector.
- data() : returns a direct pointer to the memory array used internally by the vector to store its owned elements.
Modifiers:
- assign() – It assigns new value to the vector elements by replacing old ones
- push_back() – It push the elements into a vector from the back
- pop_back() – It is used to pop or remove elements from a vector from the back.
- insert() – It inserts new elements before the element at the specified position
- erase() – It is used to remove elements from a container from the specified position or range.
- swap() – It is used to swap the contents of one vector with another vector of same type. Sizes may differ.
- clear() – It is used to remove all the elements of the vector container
- emplace() – It extends the container by inserting new element at position
- 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
ptr
All Vector Functions :
- vector::begin() and vector::end()
- vector rbegin() and rend()
- vector::cbegin() and vector::cend()
- vector::crend() and vector::crbegin()
- vector::assign()
- vector::at()
- vector::back()
- vector::capacity()
- vector::clear()
- vector::push_back()
- vector::pop_back()
- vector::empty()
- vector::erase()
- vector::size()
- vector::swap()
- vector::reserve()
- vector::resize()
- vector::shrink_to_fit()
- vector::operator=
- vector::operator[]
- vector::front()
- vector::data()
- vector::emplace_back()
- vector::emplace()
- vector::max_size()
- vector::insert()