Vector Method
vector()
A vector in C++ is a dynamic array provided by the Standard Template Library (STL).
#Syntaxβ
vector<datatype> arr;
arr={'k','o','s','h','l','e','s','h'}
The push_back() methodβ
The push_back() method adds a single element to the end of the list.
vector<int> my_vec = {1, 2, 3};
my_vec.push_back(4);
[1, 2, 3, 4]
The insert() methodβ
The insert() method with iterators can add multiple elements to the end.
vector<int> my_vec = {1, 2, 3};
vector<int> new_elements = {4, 5, 6};
my_vec.insert(my_vec.end(), new_elements.begin(), new_elements.end());
[1, 2, 3, 4, 5, 6]
The insert() at index methodβ
The insert() method inserts an element at the specified index.
vector<int> my_vec = {1, 2, 3};
my_vec.insert(my_vec.begin() + 1, 99);
[1, 99, 2, 3]
The erase() methodβ
Removes the first occurrence of a value (requires finding it first).
vector<int> my_vec = {1, 2, 3, 2, 4};
auto it = find(my_vec.begin(), my_vec.end(), 2);
if (it != my_vec.end()) {
my_vec.erase(it);
}
[1, 3, 2, 4]
The pop_back() methodβ
Removes the last element
vector<int> my_vec = {1, 2, 3};
my_vec.pop_back();
[1, 2]
The clear() methodβ
The clear() method removes all elements from the vector.
vector<int> my_vec = {1, 2, 3};
my_vec.clear();
[]
The find() methodβ
Using find() from < algorithm > to locate elements.
vector<int> my_vec = {1, 2, 3, 4, 5};
auto it = find(my_vec.begin(), my_vec.end(), 3);
if (it != my_vec.end()) {
int index = distance(my_vec.begin(), it);
}
2
The sort() methodβ
sort method arrange all the element in ascending order by default
vector<int> my_vec = {3, 1, 4, 2};
sort(my_vec.begin(), my_vec.end());
[1, 2, 3, 4]
The reverse() methodβ
Reverse all the element
vector<int> my_vec = {1, 2, 3};
reverse(my_vec.begin(), my_vec.end());
[3, 2, 1]
The front() methodβ
The front() function returns a reference to the first element of the vector.
vector<int> v = {10, 20, 30};
cout << v.front();
v.front() = 100;
cout << v[0];
[10]
[100]
The back() methodβ
The back() function returns a reference to the last element of the vector.
vector<int> v = {10, 20, 30};
cout << v.back();
[30]
The swap() methodβ
The swap() function exchanges the contents of two vectors efficiently.
vector<int> v1 = {1, 2, 3};
vector<int> v2 = {10, 20, 30};
v1.swap(v2);
v1: 10 20 30
v2: 1 2 3
The resize() methodβ
The resize() function is used to change the size of a vector.
vector<int> nums = {1, 2, 3, 4, 5};
// Increase size (new elements initialized to 0)
nums.resize(8);
// Increase size with specific value
nums.resize(10, 9);
// Decrease size
nums.resize(4);
cout << "Size: " << nums.size() << endl;
cout << "Capacity: " << nums.capacity() << endl;
nums: [1, 2, 3, 4, 5, 0, 0, 0]
nums: [1, 2, 3, 4, 5, 0, 0, 0, 9, 9]
nums: [1, 2, 3, 4]
Size: 4
Capacity: 10
The size() methodβ
Returns how many elements are currently stored in the vector.
vector<int> v = {1, 2, 3};
cout << v.size();
3
The auto() method (for Iteration)β
auto keyword allows simpler and cleaner iteration over a vector. It is especially useful in range-based for loops or when using iterators to traverse the entire vector in one pass.
vector<int> v = {10, 20, 30};
for (auto x : v) {
cout << x << " ";
}
10 20 30