Vector

Vector ąøąø°ą¹ąø«ąø”ąø·ąøąøąøąø±ąø Dynamic Arrays ąøąøµą¹ąø”ąøµąøąø§ąø²ąø”ąøŖąø²ąø”ąø²ąø£ąøą¹ąøąøąø²ąø£ąøąø£ąø±ąøąøąøąø²ąøąøąø±ąø§ą¹ąøąøą¹ąøąø¢ąøąø±ąøą¹ąøąø”ąø±ąøąø“ą¹ąø”ąø·ą¹ąøąø”ąøµąøąø²ąø£ą¹ąøąø£ąøąø«ąø£ąø·ąøąø„ąøąøąøąøą¹ąøąø£ąø°ąøąøąø ą¹ąøąø¢ąøąøąøą¹ąøąøą¹ąøąøąø£ą¹ąøąø°ąøąø±ąøąøąø²ąø£ąøąø·ą¹ąøąøąøµą¹ą¹ąøą¹ąøąøą¹ąøąø”ąø¹ąø„ą¹ąøąø¢ąøąø±ąøą¹ąøąø”ąø±ąøąø“
ąøąø²ąø£ąøąø£ąø°ąøąø²ąøØ Vector ąøąø±ą¹ąøąøŖąø²ąø”ąø²ąø£ąøąøąø³ą¹ąøą¹ą¹ąøąø¢ąøąø²ąø£ą¹ąøą¹ąøąø³ąøŖąø±ą¹ąøąøąø±ąøąøąøµą¹
// vector<data_type> vector_name;
vector<int> Hallway(9);
vector<int> Hallway;
vector<int> Hallway {1,2,3,4,5};
vector<int> Hallway = {1,2,3,4,5};
vector<double> grade;
vector <char> Alpgrade;
vector<string> cars {"Jame","Jack","John"};
ąøąø±ąøąøą¹ąøąø±ąøąøąøµą¹ą¹ąøąøµą¹ąø¢ąø§ąøą¹ąøąøąøąø±ąøą¹ąø§ąøą¹ąøąøąø£ą¹ąøąø·ąø:
ąøąø±ąøąøą¹ąøąø±ąøąøąøµą¹ąøąø¹ąøą¹ąøą¹ąøą¹ąøąø¢ąøąøµą¹ąøŖąøøąø
push_back(): appends an element to the end
pop_back() Erases the last element
size() provides the number of elements
begin() provides reference to last element
end() provides reference to end of Vector.
Iterators (ąøąø±ąø§ąø§ąøąøą¹ąø³)
begin() ā Returns an iterator pointing to the first element in the vector
end() ā Returns an iterator pointing to the theoretical element that follows the last element in the vector
rbegin() ā Returns a reverse iterator pointing to the last element in the vector (reverse beginning). It moves from last to first element
rend() ā Returns a reverse iterator pointing to the theoretical element preceding the first element in the vector (considered as reverse end)
cbegin() ā Returns a constant iterator pointing to the first element in the vector.
cend() ā Returns a constant iterator pointing to the theoretical element that follows the last element in the vector.
crbegin() ā Returns a constant reverse iterator pointing to the last element in the vector (reverse beginning). It moves from last to first element
crend() ā Returns a constant reverse iterator pointing to the theoretical element preceding the first element in the vector (considered as reverse end)
// iterators in vector
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> g1;
for (int i = 1; i <= 5; i++)
g1.push_back(i);
cout << "Output of begin and end: ";
for (auto i = g1.begin(); i != g1.end(); ++i)
cout << *i << " ";
cout << "\nOutput of cbegin and cend: ";
for (auto i = g1.cbegin(); i != g1.cend(); ++i)
cout << *i << " ";
cout << "\nOutput of rbegin and rend: ";
for (auto ir = g1.rbegin(); ir != g1.rend(); ++ir)
cout << *ir << " ";
cout << "\nOutput of crbegin and crend : ";
for (auto ir = g1.crbegin(); ir != g1.crend(); ++ir)
cout << *ir << " ";
return 0;
}
Output
Output of begin and end: 1 2 3 4 5
Output of cbegin and cend: 1 2 3 4 5
Output of rbegin and rend: 5 4 3 2 1
Output of crbegin and crend : 5 4 3 2 1
2. Capacity (ąøąø§ąø²ąø”ąøąøø)
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 to the vector expressed as number of elements.
resize(n) ā Resizes the container so that it contains ānā elements.
empty() ā Returns whether the container is empty.
shrink_to_fit() ā Reduces the capacity of the container to fit its size and destroys all elements beyond the capacity.
reserve() ā Requests that the vector capacity be at least enough to contain n elements.
// capacity function in vector
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> g1;
for (int i = 1; i <= 5; i++)
g1.push_back(i);
cout << "Size : " << g1.size();
cout << "\nCapacity : " << g1.capacity();
cout << "\nMax_Size : " << g1.max_size();
// resizes the vector size to 4
g1.resize(4);
// prints the vector size after resize()
cout << "\nSize : " << g1.size();
// checks if the vector is empty or not
if (g1.empty() == false)
cout << "\nVector is not empty";
else
cout << "\nVector is empty";
// Shrinks the vector
g1.shrink_to_fit();
cout << "\nVector elements are: ";
for (auto it = g1.begin(); it != g1.end(); it++)
cout << *it << " ";
return 0;
}
Output
Size : 5
Capacity : 8
Max_Size : 4611686018427387903
Size : 4
Vector is not empty
Vector elements are: 1 2 3 4
Reference
Last updated
Was this helpful?