Vector is a template class in STL (Standard Template Library) of C++ programming language. The vectors are sequence containers that store elements.
Syntax :
vector < type parameter > vector_name ;
The program demonstrates the use of the STL vector container in C++. First, the program creates an empty vector a.
array < int > a ;
The program then uses various member functions of the vector container to manipulate and display its contents
#include<iostream> #include<vector> using namespace std; //STL-Vector in C++ int main() { vector<int> a; cout<<"Capacity : "<<a.capacity()<<endl; a.push_back(10); cout<<"Capacity : "<<a.capacity()<<endl; a.push_back(20); cout<<"Capacity : "<<a.capacity()<<endl; a.push_back(30); cout<<"Capacity : "<<a.capacity()<<endl; cout<<"Size : "<<a.size()<<endl; cout<<"Vector Element At 2 Index : "<<a.at(2)<<endl; cout<<"Vector First Element: "<<a.front()<<endl; cout<<"Vector Last Element: "<<a.back()<<endl; cout<<"Before Pop : "<<endl; cout<<"A : "; for(int x : a) { cout<<" "<<x; } cout<<endl; a.pop_back(); cout<<"After Pop : "<<endl; cout<<"A : "; for(int x : a) { cout<<" "<<x; } cout<<endl; cout<<"Before Size : "<<a.size()<<endl; a.clear(); cout<<"After Size : "<<a.size()<<endl; cout<<"After Capacity : "<<a.capacity()<<endl; vector<int> b(5,10); cout<<"B : "; for(int x : b) { cout<<" "<<x; } vector<int> c(b); cout<<endl; cout<<"C : "; for(int x : c) { cout<<" "<<x; } cout <<endl; for (auto i = c.begin(); i != c.end(); i++) cout << *i << " "; vector<int> d(5,20); cout<<endl; cout<<"C : "; for(int x : c) { cout<<" "<<x; } cout<<endl; cout<<"D : "; for(int x : d) { cout<<" "<<x; } c.swap(d); cout<<endl; cout<<"C : "; for(int x : c) { cout<<" "<<x; } cout<<endl; cout<<"D : "; for(int x : d) { cout<<" "<<x; } return 0; }
Capacity : 0 Capacity : 1 Capacity : 2 Capacity : 4 Size : 3 Vector Element At 2 Index : 30 Vector First Element: 10 Vector Last Element: 30 Before Pop : A : 10 20 30 After Pop : A : 10 20 Before Size : 2 After Size : 0 After Capacity : 4 B : 10 10 10 10 10 C : 10 10 10 10 10 10 10 10 10 10 C : 10 10 10 10 10 D : 20 20 20 20 20 C : 20 20 20 20 20 D : 10 10 10 10 10To download raw file Click Here
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions