Vectors are sequence containers that work the same as variable-size Arrays. It is a kind of STL (Standard Template Library) based on a class template that can provide standard functionality like a Dynamic Array. Similarly, like an array, a vector can be of any type like a character array, integer array, boolean array, string array, etc.
A Template is not a class or function it is like a blueprint or a guideline for the compiler to use the template to create a specific class or function. We specify which class to instantiate by supplying additional information depending on the template in our case we will specify the vector data type in between two angle brackets after the template name.
- syntax: vector<datatype> vector_name;
- example: vector<int> myvec;(alert-success)
In a vector, elements are in contiguous memory locations, and when we insert a value in a vector stored at the end. Removing or inserting values into a vector takes a constant amount of time and their size grows dynamically as their storage is handled by a container. We need to include a specific <vector> header file in our C++ program to use all the functionality of vector.
Vector Functions.
There is a list of member functions available in the vector header file that we can directly use for solving our coding problems. Let's understand them one by one.
Iterator member functions:
Member Function | Definition |
---|---|
begin() | This function returns an iterator pointer pointing to the first element of the vector. |
end() | This function returns an iterator pointer pointing to the last element of the vector. |
rbegin() | This function returns a reverse iterator pointer pointing to the last element of the vector. It follows a reverse order and moves from the last element toward the first. |
rend() | This function returns a reverse iterator pointer pointing to the first element of the vector. It follows a reverse order and moves from the first element toward the last. |
cbegin() | This function returns a const_iterator pointing to the first element of the vector container. This const_iterator is used to point to a const value. |
cend() | This function returns a const_iterator pointing to the last element of the vector container. This const_iterator is used to point to a const value. |
crbegin() | This function returns a const_reverse_iterator pointing to the last element of the vector container. |
crend() | This function returns a const_reverse_iterator pointing to the first element of the vector container. |
//C++ Program to show iterator function of vector STL #include<iostream> #include<vector> using namespace std; int main(){ //declaration of integer type vector vector<int> myVec; //inserting elements in vector for(int i = 0; i < 6; i++) myVec.push_back(i+2); cout<<"Printing values using begin() and end() fun: "; for(auto it = myVec.begin(); it != myVec.end(); it++) cout<<*it<<" "; cout<<"\nPrinting values using rbegin() and rend(): "; for(auto it = myVec.rbegin(); it != myVec.rend(); it++) cout<<*it<<" "; cout<<"\nPrinting values using cbegin() and cend(): "; for(auto it = myVec.cbegin(); it != myVec.cend(); it++) cout<<*it<<" "; cout<<"\nPrinting values using crbegin() and crend(): "; for(auto it = myVec.crbegin(); it != myVec.crend(); it++) cout<<*it<<" "; return 0; }
Printing values using begin() and end() fun: 2 3 4 5 6 7
Printing values using rbegin() and rend(): 7 6 5 4 3 2
Printing values using cbegin() and cend(): 2 3 4 5 6 7
Printing values using crbegin() and crend(): 7 6 5 4 3 2
Vector Functions to Check Capacity.
Member Function | Definition |
---|---|
size() | This function returns the size of the vector or we can say it returns a number of elements present in the vector. |
capacity() | This function returns the current allocated storage space to the vector. This capacity might not be equal to the size of the vector. |
shrink_to_fit() | This function decreases the current capacity of the vector container and makes it equal to the size. |
resize(n) | This function changes the vector's current size to make it fit for n elements. |
reserve() | This function requests that the vector capacity be at least enough for n elements only when the current capacity is not enough for n elements. |
empty() | This function is used to check if the vector is empty or not or if the size is equal to 0 then it returns true else returns false. |
max_size() | This function gives us an idea about the maximum number of elements the vector can hold. |
//C++ Program to show capacity function of vector STL #include<iostream> #include<vector> using namespace std; int main(){ //declaration of integer type vector vector<int> myVec; //inserting elements in vector for(int i = 0; i < 6; i++) myVec.push_back(i+2); if(!myVec.empty()){ cout<<"Size: "<<myVec.size()<<endl; cout<<"Capacity: "<<myVec.capacity()<<endl; cout<<"Maximum Size: "<<myVec.max_size()<<endl; myVec.shrink_to_fit(); cout<<"Capacity after shrinking: "<<myVec.capacity()<<endl; } else{ cout<<"Vector is empty!!!"<<endl; } cout<<"Elements present in the vector: "; for(auto it = myVec.begin(); it != myVec.end(); it++) cout<<*it<<" "; return 0; }
Size: 6
Capacity: 8
Maximum Size: 230584300921363951
Capacity after shrinking: 6
Elements present in the vector: 2 3 4 5 6 7
Element Access member functions:
Member Function | Definition |
---|---|
operator[i] | This function returns a reference to the element at the position mentioned in the square bracket in the vector. |
at(i) | This function works the same as the previous one and returns a reference to the element present at position i in the vector. |
front() | This function returns a reference to the first element of the vector. |
back() | This function returns a reference to the first element of the vector. |
data() | This function returns a direct pointer to the memory array used internally by the vector to store its owned elements. |
//C++ Program to show Element access function of vector STL #include<iostream> #include<vector> using namespace std; int main(){ //declaration of integer type vector vector<int> myVec; //inserting elements in vector for(int i = 0; i < 6; i++) myVec.push_back(i+2); cout<<"Elements present in the vector: "; for(auto it = myVec.begin(); it != myVec.end(); it++) cout<<*it<<" "; cout<<"\nElement at position myVec[3]: "<<myVec[3]<<endl; myVec[3] = 10; cout<<"Element at position myVec[3]: "<<myVec.at(3)<<endl; cout<<"First element of vector: "<<myVec.front()<<endl; cout<<"Last element of vector: "<<myVec.back()<<endl; int *p = myVec.data(); cout<<"First element of vector: "<<*p; return 0; }
Elements present in the vector: 2 3 4 5 6 7
Element at position myVec[3]: 5
Element at position myVec[3]: 10
First element of vector: 2
Last element of vector: 7
First element of vector: 2
Vector Modifier member functions.
Member Function | Definition |
---|---|
push_back() | This function is used to insert or add an element at the end of the vector. |
pop_back() | This function is used to delete an element from the end of the vector. |
insert() | This function is used to insert an element before the element at the specified position. |
assign() | This function is used to assign new values to the vector elements by replacing the old values. |
erase() | This function is used to remove a single element or a range of elements from a vector. |
swap() | This function is used to swap the values of one vector with the values of other vectors of the same type. The size of both vectors might not be the same. |
clear() | This function is used to remove all the elements of the vector and empty it. |
emplace() | This function is used to insert a new element at a specific position and extend the size of the vector by one. |
emplace_back() | This function is used to insert a new element at the end position and effectively extend the size of the vector by one. |
//C++ Program to show Element access function of vector STL #include<iostream> #include<vector> using namespace std; int main(){ //declaration of integer type vector vector<int> myVec; //inserting elements in vector for(int i = 0; i < 6; i++) myVec.push_back(i+2); cout<<"Elements present in the vector: "; for(auto it = myVec.begin(); it != myVec.end(); it++) cout<<*it<<" "; myVec.push_back(50); cout<<"\nLast Element after push_back: "<<myVec.back()<<endl; //removing the last element myVec.pop_back(); cout<<"Last Element after pop_back: "<<myVec.back()<<endl; //inserting element at pos 4 myVec.insert(myVec.begin()+3, 35); cout<<"Element at position 4: "<<myVec[4]<<endl; cout<<"Size of vector before emplace: "<<myVec.size()<<endl; myVec.emplace(myVec.begin()+4, 55); cout<<"Size of vector after emplace: "<<myVec.size()<<endl; myVec.clear(); cout<<"Size of vector after clear: "<<myVec.size()<<endl; return 0; }
Elements present in the vector: 2 3 4 5 6 7
Last Element after push_back: 50
Last Element after pop_back: 7
Element at position 4: 5
Size of vector before emplace: 7
Size of vector after emplace: 8
Size of vector after clear: 0
- Accessing any random element takes a constant amount of time O(1).
- Inserting or removing elements takes a linear amount of time O(n) except for the last element which takes a constant amount of time O(1).
- Resizing the vector size in an operation like insert or emplace takes a linear amount of time O(n).
No comments:
Post a Comment