
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to Implement Vector
A vector is a dynamic array that can resize itself if an element is inserted or deleted. The vector elements are contained in a contiguous storage and the container handles the storage automatically.
A program that implements vectors is given as follows −
Example
#include <iostream> #include <vector> #include <string> #include <cstdlib> using namespace std; int main() { int ch, val; vector<int> vec; cout<<"1)Insert Element into the Vector"<<endl; cout<<"2)Delete Last Element of the Vector"<<endl; cout<<"3)Print size of the Vector"<<endl; cout<<"4)Display Vector elements"<<endl; cout<<"5)Clear the Vector"<<endl; cout<<"6)Exit"<<endl; do { cout<<"Enter your Choice: "<<endl; cin>>ch; switch(ch) { case 1: cout<<"Enter value to be inserted: "<<endl; cin>>val; vec.push_back(val); break; case 2: cout<<"Last Element is deleted."<<endl; vec.pop_back(); break; case 3: cout<<"Size of Vector: "; cout<<vec.size()<<endl; break; case 4: cout<<"Displaying Vector Elements: "; for (int i = 0; i < vec.size(); i++) cout<<vec[i]<<" "; cout<<endl; break; case 5: vec.clear(); cout<<"Vector Cleared"<<endl; break; case 6: cout<<"Exit"<<endl; break; default: cout<<"Error....Wrong Choice Entered"<<endl; } } while (ch!=6); return 0; }
Output
The output of the above program is as follows
1)Insert Element into the Vector 2)Delete Last Element of the Vector 3)Print size of the Vector 4)Display Vector elements 5)Clear the Vector 6)Exit Enter your Choice: 1 Enter value to be inserted: 5 Enter your Choice: 1 Enter value to be inserted: 2 Enter your Choice: 1 Enter value to be inserted: 8 Enter your Choice: 1 Enter value to be inserted: 6 Enter your Choice: 3 Size of Vector: 4 Enter your Choice: 4 Displaying Vector Elements: 5 2 8 6 Enter your Choice: 2 Last Element is deleted. Enter your Choice: 3 Size of Vector: 3 Enter your Choice: 4 Displaying Vector Elements: 5 2 8 Enter your Choice: 5 Vector Cleared Enter your Choice: 3 Size of Vector: 0 Enter your Choice: 4 Displaying Vector Elements: Enter your Choice: 9 Error....Wrong Choice Entered Enter your Choice: 6 Exit
In the above program, first the vector is defined and then a menu is provided to the user to choose the vector operations. This is given below −
vector<int> vec; cout<<"1)Insert Element into the Vector"<<endl; cout<<"2)Delete Last Element of the Vector"<<endl; cout<<"3)Print size of the Vector"<<endl; cout<<"4)Display Vector elements"<<endl; cout<<"5)Clear the Vector"<<endl; cout<<"6)Exit"<<endl;
A do while loop is used to enter the user choice and a switch statement is used to implement the operations according to the choice. The different operations are insert element into vector, delete element from vector, print size of vector, display elements of vector, clear vector and exit. The code snippet for this is given below −
do { cout<<"Enter your Choice: "<<endl; cin>>ch; switch(ch) { case 1: cout<<"Enter value to be inserted: "<<endl; cin>>val; vec.push_back(val); break; case 2: cout<<"Last Element is deleted."<<endl; vec.pop_back(); break; case 3: cout<<"Size of Vector: "; cout<<vec.size()<<endl; break; case 4: cout<<"Displaying Vector Elements: "; for (int i = 0; i < vec.size(); i++) cout<<vec[i]<<" "; cout<<endl; break; case 5: vec.clear(); cout<<"Vector Cleared"<<endl; break; case 6: cout<<"Exit"<<endl; break; default: cout<<"Error....Wrong Choice Entered"<<endl; } } while (ch!=6);
- Related Articles
- C++ Program to Implement Vector in STL
- Using class to implement Vector Quantities in C++
- C# program to implement FizzBuzz
- C++ Program to Implement Trie
- C++ Program to Implement Stack
- C++ Program to Implement Treap
- C++ Program to Implement Dequeue
- C++ Program to Implement Queue
- Java Program to Implement LinkedList
- C program to implement CHECKSUM
- C++ Program to Convert Vector to a List
- Golang program to convert vector to a list
- Java program to implement bubble sort
- Java program to implement selection sort
- Java program to implement insertion sort
