Table of Contents
Vector
Vector is a template class in the C++ Standard Template Library (STL) that represents an array of elements. It is similar to an array but with the ability to dynamically grow or shrink in size.
Capacity
The capacity of a vector is the amount of memory allocated for the elements of the vector. The capacity of a vector can be greater than its size, which means that it has some extra space to accommodate new elements without having to reallocate memory.
Code for Capacity function in C++:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> v;
cout << "Capacity : " << v.capacity() << endl;
for (int i = 1; i <= 5; i++)
v.push_back(i);
cout << "Capacity : " << v.capacity() << endl;
return 0;
}
Element Access
Vector elements can be accessed using the [] operator or the at() function. The [] operator can be used to access the elements of a vector just like an array. The at() function can also be used to access the elements of a vector. It is similar to the [] operator but it also performs range checking, which means it throws an exception if the index is out of range.
Modifiers
- push_back() function is used to insert an element at the end of the vector.
- pop_back() function is used to remove the last element of the vector.
- insert() function is used to insert an element at a specific position in the vector.
- erase() function is used to remove an element or a range of elements from a specific position in the vector.
C++ program to illustrate the Modifiers in vector:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> v;
v.push_back(10);
v.push_back(20);
v.push_back(30);
v.push_back(40);
cout << "Vector elements are: ";
for (int i = 0; i < v.size(); i++)
cout << v[i] << " ";
cout << endl;
v.pop_back();
cout << "Vector elements after pop_back(): ";
for (int i = 0; i < v.size(); i++)
cout << v[i] << " ";
cout << endl;
v.insert(v.begin() + 2, 35);
cout << "Vector elements after insert(): ";
for (int i = 0; i < v.size(); i++)
cout << v[i] << " ";
cout << endl;
v.erase(v.begin() + 1);
cout << "Vector elements after erase(): ";
for (int i = 0; i < v.size(); i++)
cout << v[i] << " ";
cout << endl;
return 0;
}
ALL VECTOR FUNCTIONS:
CONSTRUCTORS:
- vector(): Constructs an empty vector.
- vector(n): Constructs a vector with n elements.
- vector(n, val): Constructs a vector with n elements, each element is initialized to val.
- vector(begin, end): Constructs a vector with elements in the range [begin, end].
CAPACITY FUNCTIONS:
- size(): Returns the number of elements in the vector.
- max_size(): Returns the maximum number of elements that the vector can hold.
- capacity(): Returns the size of the storage space currently allocated for the vector, measured in terms of elements.
- resize(n): Resizes the vector to contain n elements.
- empty(): Returns whether the vector is empty (i.e. whether its size is 0).
- reserve(n): Requests that the vector capacity be at least enough to contain n elements.
ELEMENT ACCESS FUNCTIONS:
- operator: Returns a reference to the element at position n in the vector.
- at(): Returns a reference to the element at position n in the vector.
- front(): Returns a reference to the first element of the vector.
- back(): Returns a reference to the last element of the vector.
- data(): Returns a pointer to the first element of the vector.
MODIFIERS:
- push_back(val): Adds a new element to the end of the vector.
- pop_back(): Removes the last element of the vector.
- insert(pos, val): Inserts an element at position pos in the vector.
- insert(pos, n, val): Inserts n copies of an element at position pos in the vector.
- insert(pos, begin, end): Inserts a range of elements at position pos in the vector.
- erase(pos): Removes an element at position pos from the vector.
- erase(begin, end): Removes a range of elements from the vector.
- swap(v): Swaps the contents of the vector with the contents of v.
- clear(): Clears all elements of the vector.
ITERATORS:
- begin(): Returns an iterator pointing to the first element of the vector.
- end(): Returns an iterator pointing to the last element of the vector.
- rbegin(): Returns a reverse iterator pointing to the last element of the vector.
- rend(): Returns a reverse iterator pointing to the first element of the vector.
CAPACITY:
- capacity(): Returns the size of the currently allocated storage.
- reserve(): Requests a change in capacity.
- shrink_to_fit(): Reduces memory usage by freeing unused memory.