Vectors in C++ are the same as dynamic arrays and will dynamically resize themselves as an element is added or removed. Vectors implemented to handle a variety of elements at a time, so that the size of a vector may vary, while array classes have a fixed size. Vector elements are put in contiguous storage for accessed and traverse using iterators.
Vector insertion is not always constant. When we insert an element at the end of the vector if a vector is full, it takes time to resize and time to insert an element just when it inserted at the end. Therefore the insertion time is not always constant. Vectors have a dynamic size. They have placed in contiguous memory for easy access.
When the vector is full, i.e. when the elements in the vector are equal to the vector’s capacity, then the vector is doubled, i.e. if it is 2 before the capacity, then it is 2 * 2 = 4 + 2 = 4.
Difference between vector and array in C++
An array takes a static method such that at runtime, its size cannot be modified. While Vector applies the dynamic array, which ensures it immediately resizes after inserting elements.
How are vectors stored in C++?
Vectors are declared with the following syntax:
std::vector <type> variable (elements)
Example:
#include <vector> int main() { std::vector<int> my_vector; }
• type describes a vector data type (i.e., <int >, < double >, or < string >)
• Variable is a name for the data that you choose
• Elements specified the number of data elements
Below are examples of C++ vector:
vector<int> values (15); // Declares a vector of 15 integers vector<double> marks (10); // Declares a vector of 10 doubles vector<string> Snames; // Declares a vector of strings, // initially empty (contains 0 strings)
//examples of vector in C++ #include <iostream> #include <vector> using namespace std; int main() { vector<double> marks(10); for(vector<double>::size_type i = 0; i < 10; i++) { cout << "Enter student marks : " << i+1 << ": " << flush; cin >> marks[i]; } return 0; }
In the first sentence, a vector declared named marks that contain 10 values of double type. These values can be accessed as marks[0] to marks[10]. The counter for loop goes from 0 to 10 and enables sequential access to each specific element.
The class vector <type> provides a size_type to represent positions and sizes as with strings. You are always advised to use this data type while working with vectors.
For loops, vectors or arrays typically access each element one by one using a variable of loop control as a subscript. The variable name and type must specify. However, the number of elements is optional.
Member Functions of Vectors in C++
We have different useful Vector Functions in C++
• Modifiers
• Iterators
• Capacity
C++ Vector Iterators
Certain functions associated with the vector are:
Function | Description |
begin() | Returns an iterator that points to the first vector element. |
end() | It refers to the element following the vector last element. |
rbegin() | It points the vector’s last element (reverse start). It shifts from the last element to the first. |
rend() | It refer to the element preceding the vector’s first element. |
cbegin() | It refers to the first vector element. |
cend() | It refers to the after the last vector element. |
crbegin() | It refers to the last node of the vector. It shifts from the last element to the first. |
crend() | It refers to the element that precedes the first element in the vector. |
// C++ program to illustrate the iterators in vector #include <iostream> #include <vector> using namespace std; int main() { vector<int> v1; for (int i = 1; i <= 5; i++) v1.push_back(i); cout << "Output of begin and end: "; for (auto i = v1.begin(); i != v1.end(); ++i) cout << *i << " "; cout << "\nOutput of cbegin and cend: "; for (auto i = v1.cbegin(); i != v1.cend(); ++i) cout << *i << " "; cout << "\nOutput of rbegin and rend: "; for (auto ir = v1.rbegin(); ir != v1.rend(); ++ir) cout << *ir << " "; cout << "\nOutput of crbegin and crend : "; for (auto ir = v1.crbegin(); ir != v1.crend(); ++ir) cout << *ir << " "; return 0; }
Vector Modifiers Functions
As its name indicates, a modifier may use to modify the context of a specific type of data. Here are several modifiers that can found in C++ vector:
Function | Description |
push_back() | pushes elements from the back. |
insert() | inserts new elements to a specified location. |
pop_back() | removes elements from the back. |
erase() | removes a range of elements from a specified location. |
clear() | removes all elements. |
assign() | The vector elements are assigned a new value by deleting the old elements. |
swap() | Used to swap the contents of one vector to another of the same type. Sizes can vary. |
// Example of Modifiers in vector c++ #include<iostream> #include<vector> using namespace std; int main() { vector<string> v1; v1.push_back("My"); v1.push_back(" Notes"); for(vector<string>::iterator itr=v1.begin();itr!=v1.end();++itr) cout<<*itr; return 0; }
Vector Capacity Functions
Function | Description |
size() | It returns the number of elements in the vector. |
max_size() | The maximum number of elements the vector can hold. |
capacity() | The function returns the vector’s storage size currently assigned as some elements depending on the vector’s memory. |
resize() | This function resizes the container to include ‘n’ elements. If the vector size is greater than n, the back elements omit from the vector, and If it is less than n, additional elements add at the back of the vector. |
empty() | Returns if the container is empty. If the vector is empty, it returns false. |
// Example of Capacity in vector c++ #include <iostream> #include <vector> using namespace std; int main() { vector<int> v1; for (int i = 1; i <= 10; i++) v1.push_back(i); cout << "Size of Vector: " << v1.size()<<endl; cout << "Capacity of Vector: " << v1.capacity()<<endl; cout << "Max_Size of vector: " << v1.max_size()<<endl; v1.resize(5); // resizes the vector size to 5 // prints the vector size after resize() cout << "Size of vector after resize: " << v1.size()<<endl; // checks if the vector is empty or not if (v1.empty() == false) cout << "Vector is not empty"; else cout << "Vector is empty"; return 0; }