Here you will find out about C++ STL Vector Container, for example, std:: vector and different capacities material on it.
Vector, as we talked about before vector is a dynamic exhibit that develops one way.
C++ STL Vector
Vectors are dynamic clusters. At the point when we embed another component or erase a component from the vector, it has the capacity to resize itself naturally.
Vector components likewise put away in adjacent memory areas, with the goal that they can navigate and get to by iterators. Embeddings and erasing at the end takes consistent, O(1) time. By utilizing vectors we can stay away from array_index_outof_bound special cases.
The language structure of STL Vector
vector vector name;
Capacities Used with Vector Container
push_back(): This is utilized to embed a component into a vector. It embeds component toward the finish of the vector.
[] administrator: This administrator restores the reference of the component situated at the file where we access like vectorName[index_position].
at(): This administrator additionally helpful to get to the component at a specific position.
front(): This profits the reference to the primary component of the vector.
back(): This profits the reference to the last component of the vector.
Model program to appear previously mentioned capacities:
#include<iostream>
#include<vector>
using namespace std;
int main(){
vector<int> vec; // syntax for defining a vector
// inserting elements into vector by push_back funtion
for(int i=0;i<6;i++)
vec.push_back(i);
// accessing using [] operator
for(int i=0;i<3;i++)
cout<< vec[i] << " ";
// accessing using at()
for(int i=3;i<6;i++)
cout << vec.at(i) << " ";
cout << endl;
// returning front element
cout << vec.front() << endl;
//returning last element
cout << vec.back() << endl;
return 0;
}
Output
0 1 2 3 4 5
0
5
size(): It gives the number of components present in the vector.
max_size(): It gives the greatest number of components that a vector can hold.
limit(): We said that the vector is a dynamic exhibit which develops by embeddings components.
At the point when we pronounce it, framework allots some space to it. A specific number of components it can hold.
On the off chance that we embed more than that components, framework assigns some more space for it (at the new area by free-up old space).
Limit() returns what number of things can be fit in the vector before it is “full”. When full, including new things, will bring about another, the bigger square of memory being distributed and the current things being duplicated to it.
resize(): It resizes the vector, confines to contain just some number of components.
void(): This is Boolean work. Returns whether the vector is vacant or not.
Model Program to appear above capacities:
#include<iostream>
#include<vector>
using namespace std;
int main(){
vector<int> vec;
for(int i=0;i<5;i++) vec.push_back(i);
cout << "size of the vector is " << vec.size() << endl; // present number of elements
cout << "Maximum size is " << vec.max_size() << endl; // maximum number of elements can hold
cout << "Capacity of the vector is " << vec.capacity() << endl; //
vec.resize(0); // restricting vector to contain zero elements
vec.empty() ? cout << "vector is empty" << endl: cout << "vector is not empty " << endl; //checking empty conditon
return 0;
}
Output
size of the vector is 5
Maximum size is 4611686018427387903
Capacity of the vector is 8
vector is empty
Vector with Iterator
start(): Returns an iterator indicating the main component of the vector.
end(): Returns an iterator indicating the present last component of the vector.
begin(): Returns an invert iterator indicating the last component in the vector. Used to move from last to first.
sever(): Returns a switch iterator indicating the primary component in the vector.
Model program:
#include<iostream>
#include<vector>
using namespace std;
int main(){
vector<int> vec;
vector<int> :: iterator it1;
vector<int> :: reverse_iterator it2;
for(int i=0;i<5;i++) vec.push_back(i);
// elements form start to end
cout << "elements in the vector from start to end ";
for(it1 = vec.begin(); it1!= vec.end();it1++)
cout << *it1 << " ";
cout << endl;
// elements form end to start
cout << "elements in the vector from end to start ";
for(it2 = vec.rbegin(); it2!= vec.rend();it2++)
cout << *it2 << " ";
cout << endl;
return 0;
}
Output
elements in the vector from start to end 0 1 2 3 4
elements in the vector from end to start 4 3 2 1 0
allocate(): Assign new substance to vector and resize.
pop_back(): This evacuates the component toward the finish of the vector. So, the size of the vector likewise decreased by 1.
insert(iterator, component): This embeds the component in the vector before the position pointed by the iterator.
This addition technique can be over-burden by third-factor tally. This says how often the component to be embedded before the pointed position.
Model program to appear above techniques:
#include<iostream>
#include<vector>
using namespace std;
int main(){
vector <int> vec1;
vector <int> vec2;
vector <int> :: iterator it;
vec1.assign(4,100); // inserting element 100 into vector 4 times.
it = vec1.begin();
vec2.assign(it+1, vec1.end()); // inserts 3 elements of vec1
cout << "Vector1 elements are " << endl;
for(int i=0;i< vec1.size();i++)
cout << vec1[i] << " ";
cout << endl;
cout << "Vector2 elements are " << endl;
for(int i=0;i< vec2.size();i++)
cout << vec2[i] << " ";
cout << endl;
vec2.push_back(10);
cout << "new value inserted into vector2. Last element is " << vec2.back() << endl;
vec2.pop_back();
cout << "after pop_back operation last element of vector2 is " << vec2.back() << endl;
vector <int> vec3(3,10);
it = vec3.begin();
it = vec3.insert(it,20); // this inserts element 20 as first element
cout << "Now first element of vec3 is " << vec3.front();
return 0;
}
Output
Vector1 elements are
100 100 100 100
Vector2 elements are
100 100 100
new value inserted into vector2. Last element is 10
after pop_back operation last element of vector2 is 100
Now first element of vec3 is 20
delete(): Removes the component pointed by the iterator position. This eradicates strategy can be over-burden with an extra iterator that determines the range to be evacuated.
Model program:
#include<iostream>
#include<vector>
using namespace std;
int main(){
vector <int> vec;
vector <int> :: iterator it;
vec.push_back(100);
for(int i=0;i<5;i++)
vec.push_back(i);
cout << "first element before erasing is " << vec.front() << endl;
it = vec.begin();
vec.erase(it); // removes first element of the vector
cout << "first element after erasing is " << vec.front() << endl;
vec.erase(vec.begin(), vec.end()); // this removes elements in the vector from first to last
//checking vector empty or not
vec.empty() ? cout << "vector is empty " << endl : cout << "Vector is not empty " <<endl;
return 0;
}
Output
first element before erasing is 100
first element after erasing is 0
vector is empty
swap( vector1, vector2): This swaps all components of vector1 to vector2 and vector2 to vector1.
clear(): This expels all components of the vector.
Model program:
#include<iostream>
#include<vector>
using namespace std;
int main(){
vector <int> vec1;
vector <int> vec2;
for(int i=1;i<6;i++)
vec1.push_back(i);
for(int i=11;i<16;i++)
vec2.push_back(i);
cout << "Vector1 elements before swapping are " << endl;
for(int i=0;i<5;i++)
cout << vec1[i] << " ";
cout << endl;
cout << "Vector2 elements before swapping are " << endl;
for(int i=0;i<5;i++)
cout << vec2[i] << " ";
cout << endl;
swap(vec1,vec2);
cout << "Vector1 elements after swapping are " << endl;
for(int i=0;i<5;i++)
cout << vec1[i] << " ";
cout << endl;
cout << "Vector2 elements after swapping are " << endl;
for(int i=0;i<5;i++)
cout << vec2[i] << " ";
cout << endl;
// clearing vector1
vec1.clear();
//checking vector empty or not
vec1.empty() ? cout << "vector is empty " << endl : cout << "Vector is not empty " <<endl;
return 0;
}
Output
Vector1 elements before swapping are
1 2 3 4 5
Vector2 elements before swapping are
11 12 13 14 15
Vector1 elements after swapping are
11 12 13 14 15
Vector2 elements after swapping are
1 2 3 4 5
vector is empty
Remark underneath in the event that you have inquiries or discovered any data wrong in above instructional exercise for C++ stl vector holder.