## Vector:

Vectors are sequence containers representing arrays that can change in size.

Just like arrays, vectors use contiguous storage locations for their elements, which means that their elements can also be accessed using offsets on regular pointers to its elements, and just as efficiently as in arrays. But unlike arrays, their size can change dynamically, with their storage being handled automatically by the container.

Internally, vectors use a dynamically allocated array to store their elements. This array may need to be reallocated in order to grow in size when new elements are inserted, which implies allocating a new array and moving all elements to it. This is a relatively expensive task in terms of processing time, and thus, vectors do not reallocate each time an element is added to the container.

#### C++ Program for Vector implementation in STL:

#include <iostream> #include <vector> #include <string> #include <cstdlib> using namespace std; int main() { vector<int> ss; vector<int>::iterator it; int choice, item; while (1) { cout<<"n---------------------"<<endl; cout<<"Vector Implementation in Stl"<<endl; cout<<"n---------------------"<<endl; cout<<"1.Insert Element into the Vector"<<endl; cout<<"2.Delete Last Element of the Vector"<<endl; cout<<"3.Size of the Vector"<<endl; cout<<"4.Display by Index"<<endl; cout<<"5.Dislplay by Iterator"<<endl; cout<<"6.Clear the Vector"<<endl; cout<<"7.Exit"<<endl; cout<<"Enter your Choice: "; cin>>choice; switch(choice) { case 1: cout<<"Enter value to be inserted: "; cin>>item; ss.push_back(item); break; case 2: cout<<"Delete Last Element Inserted:"<<endl; ss.pop_back(); break; case 3: cout<<"Size of Vector: "; cout<<ss.size()<<endl; break; case 4: cout<<"Displaying Vector by Index: "; for (int i = 0; i < ss.size(); i++) { cout<<ss[i]<<" "; } cout<<endl; break; case 5: cout<<"Displaying Vector by Iterator: "; for (it = ss.begin(); it != ss.end(); it++) { cout<<*it<<" "; } cout<<endl; break; case 6: ss.clear(); cout<<"Vector Cleared"<<endl; break; case 7: exit(1); break; default: cout<<"Wrong Choice"<<endl; } } return 0; }

### Sample Output:

--------------------- Vector Implementation in Stl --------------------- 1.Insert Element into the Vector 2.Delete Last Element of the Vector 3.Size of the Vector 4.Display by Index 5.Dislplay by Iterator 6.Clear the Vector 7.Exit Enter your Choice: 1 Enter value to be inserted: 4 --------------------- Vector Implementation in Stl --------------------- 1.Insert Element into the Vector 2.Delete Last Element of the Vector 3.Size of the Vector 4.Display by Index 5.Dislplay by Iterator 6.Clear the Vector 7.Exit Enter your Choice: 1 Enter value to be inserted: 6 --------------------- Vector Implementation in Stl --------------------- 1.Insert Element into the Vector 2.Delete Last Element of the Vector 3.Size of the Vector 4.Display by Index 5.Dislplay by Iterator 6.Clear the Vector 7.Exit Enter your Choice: 1 Enter value to be inserted: 3 --------------------- Vector Implementation in Stl --------------------- 1.Insert Element into the Vector 2.Delete Last Element of the Vector 3.Size of the Vector 4.Display by Index 5.Dislplay by Iterator 6.Clear the Vector 7.Exit Enter your Choice: 1 Enter value to be inserted: 8 --------------------- Vector Implementation in Stl --------------------- 1.Insert Element into the Vector 2.Delete Last Element of the Vector 3.Size of the Vector 4.Display by Index 5.Dislplay by Iterator 6.Clear the Vector 7.Exit Enter your Choice: 1 Enter value to be inserted: 9 --------------------- Vector Implementation in Stl --------------------- 1.Insert Element into the Vector 2.Delete Last Element of the Vector 3.Size of the Vector 4.Display by Index 5.Dislplay by Iterator 6.Clear the Vector 7.Exit Enter your Choice: 1 Enter value to be inserted: 2 --------------------- Vector Implementation in Stl --------------------- 1.Insert Element into the Vector 2.Delete Last Element of the Vector 3.Size of the Vector 4.Display by Index 5.Dislplay by Iterator 6.Clear the Vector 7.Exit Enter your Choice: 3 Size of Vector: 6 --------------------- Vector Implementation in Stl --------------------- 1.Insert Element into the Vector 2.Delete Last Element of the Vector 3.Size of the Vector 4.Display by Index 5.Dislplay by Iterator 6.Clear the Vector 7.Exit Enter your Choice: 4 Displaying Vector by Index: 4 6 3 8 9 2 --------------------- Vector Implementation in Stl --------------------- 1.Insert Element into the Vector 2.Delete Last Element of the Vector 3.Size of the Vector 4.Display by Index 5.Dislplay by Iterator 6.Clear the Vector 7.Exit Enter your Choice: 2 Delete Last Element Inserted: --------------------- Vector Implementation in Stl --------------------- 1.Insert Element into the Vector 2.Delete Last Element of the Vector 3.Size of the Vector 4.Display by Index 5.Dislplay by Iterator 6.Clear the Vector 7.Exit Enter your Choice: 3 Size of Vector: 5 --------------------- Vector Implementation in Stl --------------------- 1.Insert Element into the Vector 2.Delete Last Element of the Vector 3.Size of the Vector 4.Display by Index 5.Dislplay by Iterator 6.Clear the Vector 7.Exit Enter your Choice: 5 Displaying Vector by Iterator: 4 6 3 8 9 --------------------- Vector Implementation in Stl --------------------- 1.Insert Element into the Vector 2.Delete Last Element of the Vector 3.Size of the Vector 4.Display by Index 5.Dislplay by Iterator 6.Clear the Vector 7.Exit Enter your Choice: 4 Displaying Vector by Index: 4 6 3 8 9 --------------------- Vector Implementation in Stl --------------------- 1.Insert Element into the Vector 2.Delete Last Element of the Vector 3.Size of the Vector 4.Display by Index 5.Dislplay by Iterator 6.Clear the Vector 7.Exit Enter your Choice: 6 Vector Cleared --------------------- Vector Implementation in Stl --------------------- 1.Insert Element into the Vector 2.Delete Last Element of the Vector 3.Size of the Vector 4.Display by Index 5.Dislplay by Iterator 6.Clear the Vector 7.Exit Enter your Choice: 3 Size of Vector: 0 --------------------- Vector Implementation in Stl --------------------- 1.Insert Element into the Vector 2.Delete Last Element of the Vector 3.Size of the Vector 4.Display by Index 5.Dislplay by Iterator 6.Clear the Vector 7.Exit Enter your Choice: 7 ------------------