The C++ Standard Library allows us to use vectors, which are sequence containers that represent resizable arrays. Vectors use contiguous memory locations for their elements and thus, allow us to access elements using offsets on pointers. C++ vectors automatically manage storage and are efficient if data is often added or deleted. However, they might consume more memory than an array.
Vectors have four constructors i.e. four ways to initialize
1.Empty constructor (default)
This creates an empty vector
vector<data_type> vector_name;
2.Fill constructor
Creates a vector with n elements. Each element is a copy of the value specified.
vector<int> v(5, 10); //vector with 5 ints with value 10
3.Range constructor
It constructs a from another vector from the range [first,last), in the same order.
vector<int> v1(5,10); vector<int> v2(v1.begin(), v1.begin()+3); //creates a vector v2 which contains elements of v1 from index 0 to 2
4.Copy constructor
Copies one vector to another
vector<int> v1(5,20); vector<int> v2(v1); v2 has all the elements of v1 in the same order
Here are various iterators and functions associated with vectors in the C++ Standard Library
Iterators
They are used to traverse a vector and are similar to array pointers.
1. vector::begin()
: Returns an iterator pointing to the first element in the vector.
2. vector::end()
: Returns an iterator pointing to an imaginary element after the last element in the vector.
3. vector::rbegin()
: Returns a reverse iterator pointing to the last element in the vector.
4. vector::rend()
: Returns a reverse iterator pointing to an imaginary element before the first element.
Example
#include <iostream> #include<vector> using namespace std; int main () { vector<int> vec; for(int i=1;i<5;i++) vec.push_back(i); cout<<"The elements in standard order are: \n"; for (vector<int>::iterator it = vec.begin() ; it != vec.end(); ++it) cout<<*it<<" "; cout<<endl; cout<<"The elements in reverse order are: \n"; for (vector<int>::reverse_iterator it = vec.rbegin() ; it != vec.rend(); ++it) cout<<*it<<" "; return 0; }
Output:
The elements in standard order are: 1 2 3 4 The elements in reverse order are: 4 3 2 1
Element access
There are various methods that can be used to access elements of a vector.
- reference operator
[n]
: Returns a reference to the element at position n. vector::at(n)
: Returns a reference to the element at position n.vector::front()
: Returns a reference to the first element in the vector.vector::back()
: Returns a reference to the last element in the vector.vector::data()
: Returns a pointer to the first element in the array used internally by the vector.
Example
#include <iostream> #include <vector> using namespace std; int main () { vector<int> vec; for(int i=1;i<=5;i++) vec.push_back(i); cout<<"Vector using reference []: "; for(int i=0;i<5;i++) cout<<vec[i]<<" "; cout<<"\n\n"; cout<<"First element of vector is: "<<vec.front()<<endl; cout<<"Last element of vector is: "<<vec.back()<<endl; cout<<"Element at index 3 is: "<<vec.at(3)<<endl; int *first_ele=vec.data(); cout<<"First element: "<< *first_ele; }
Output:
Vector using reference []: 1 2 3 4 5 First element of vector is: 1 Last element of vector is: 5 Element at index 3 is: 4 First ele: 1
Modifiers
Modifiers are used to change the meaning of a specified type of data. Here are some modifiers used in C++ vectors:
vector::assign()
: Assigns new contents to the vector, replacing its current contentsvector::push_back()
: Pushes an element to the back.vector::insert()
: Inserts new elements to a specified location.vector::pop_back()
: Removes elements from the back.vector::erase()
: Removes a range of elements from a specified location.vector::clear()
: Removes all elements i.e. empties the vectorvector::swap()
: Swap the contents of one vector with another vector (of the same type).
Example
#include <iostream> #include <vector> using namespace std; int main () { vector<int> vec; vec.assign(6,12); cout<<"Vector after assign: "; for(vector<int>::iterator it=vec.begin();it!=vec.end();++it) cout<<*it<<" "; cout<<"\n\n"; vec.clear(); //clearing vector for(int i=1;i<=8;i++) vec.push_back(i); cout<<"Vector after clearing then pushing elements: "; for(vector<int>::iterator it=vec.begin();it!=vec.end();++it) cout<<*it<<" "; cout<<"\n\n"; vec.pop_back(); cout<<"Vector after popping element: "; for(vector<int>::iterator it=vec.begin();it!=vec.end();++it) cout<<*it<<" "; cout<<"\n\n"; vec.insert(vec.begin()+3,20); cout<<"Vector after inserting element: "; for(vector<int>::iterator it=vec.begin();it!=vec.end();++it) cout<<*it<<" "; cout<<"\n\n"; vec.erase(vec.begin()+4); cout<<"Vector after erasing element: "; for(vector<int>::iterator it=vec.begin();it!=vec.end();++it) cout<<*it<<" "; cout<<"\n\n"; vector<int> vec2; vec2.assign(4,5); cout<<"Vector 1 before swap: "; for(vector<int>::iterator it=vec.begin();it!=vec.end();++it) cout<<*it<<" "; cout<<"\n\n"; cout<<"Vector 2 before swap: "; for(vector<int>::iterator it=vec2.begin();it!=vec2.end();++it) cout<<*it<<" "; cout<<"\n\n"; vec2.swap(vec); cout<<"Vector 1 after swap: "; for(vector<int>::iterator it=vec.begin();it!=vec.end();++it) cout<<*it<<" "; cout<<"\n\n"; cout<<"Vector 2 after swap: "; for(vector<int>::iterator it=vec2.begin();it!=vec2.end();++it) cout<<*it<<" "; cout<<"\n"; }
Output:
Vector after assign: 12 12 12 12 12 12 Vector after clearing then pushing elements: 1 2 3 4 5 6 7 8 Vector after popping element: 1 2 3 4 5 6 7 Vector after inserting element: 1 2 3 20 4 5 6 7 Vector after erasing element: 1 2 3 20 5 6 7 Vector 1 before swap: 1 2 3 20 5 6 7 Vector 2 before swap: 5 5 5 5 Vector 1 after swap: 5 5 5 5 Vector 2 after swap: 1 2 3 20 5 6 7
Capacity
There are various methods pertaining to the capacity of a vector
vector::size()
: Returns the size of a vector.vector::max_size()
: Returns the maximum size of a vector.vector::capacity()
: Returns the size of allocated storage capacity (in terms of number of elements)vector::resize(n)
: Resizes the vector so that it contains ‘n’ elements.vector::empty()
: Returns whether the vector is empty.vector::shrink_to_fit()
: Reduces the capacity of the container to fit its size and destroys extra space.vector::reserve()
: Reserves space that is at least enough to contain n elements.
Example
#include <iostream> #include<vector> using namespace std; int main () { vector<int> vec; for(int i=1;i<5;i++) vec.push_back(i); cout<<"Size of vector: "<<vec.size()<<endl; cout<<"Max size of vector: "<<vec.max_size()<<endl; cout<<"Capacity of vector: "<<vec.capacity()<<endl; cout<<endl; cout<<"Vector before resizing: \n"; for(vector<int>::iterator it=vec.begin();it!=vec.end();++it) cout<<*it<<" "; cout<<"\n"; vec.resize(3); cout<<"Vector after resizing: \n"; for(vector<int>::iterator it=vec.begin();it!=vec.end();++it) cout<<*it<<" "; cout<<"\n\n"; cout<<"Is vector empty? "<<vec.empty(); //0=no, 1=yes }
Output
Size of vector: 4 Max size of vector: 1073741823 Capacity of vector: 4 Vector before resizing: 1 2 3 4 Vector after resizing: 1 2 3 Is vector empty? 0