
- 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 Array in STL
Different operations on array and pseudocodes:
Begin In main(), While TRUE do Prints some choices. Take input of choice. Start the switch case When case is 1 Print the size of the array. Break When case is 2 Insert the values in array. Break When case is 3 Print the front element array. Break When case is 4 Print the back element of array. Break When case is 5 Print all the elements of the array. Break When case is 6 Exit. When case is default Print wrong input. End.
Example
#include <iostream> #include <array> #include <cstdlib> using namespace std; int main() { array<int, 7> a; //declaring array array<int, 7>::iterator it; //declaring an array as iterator int c, i; //declaring integer variables a.fill(0); int cnt = 0; while (1) { cout<<"1.Size of the array"<<endl; cout<<"2.Insert Element into the Array"<<endl; cout<<"3.Front Element of the Array"<<endl; cout<<"4.Back Element of the Array"<<endl; cout<<"5.Display elements of the Array"<<endl; cout<<"6.Exit"<<endl; cout<<"Enter your Choice: "; cin>>c; switch(c) { case 1: cout<<"Size of the Array: "; // print size of the array. cout<<a.size()<<endl; break; case 2: cout<<"Enter value to be inserted: "; // enter values in the array. cin>>i; a.at(cnt) = i; cnt++; break; case 3: cout<<"Front Element of the Array: "; //print front element of the array. cout<<a.front()<<endl; break; case 4: cout<<"Back Element of the Arrary: "; //print back element of the array. cout<<a.back()<<endl; break; case 5: for (it = a.begin(); it != a.end(); ++it ) //print all elements of the array. cout <<" "<< *it; cout<<endl; break; case 6: exit(1); //exit break; default: cout<<"Wrong Choice"<<endl; } } return 0; }
Output
1.Size of the array 2.Insert Element into the Array 3.Front Element of the Array 4.Back Element of the Array 5.Display elements of the Array 6.Exit Enter your Choice: 1 Size of the Array: 7 1.Size of the array 2.Insert Element into the Array 3.Front Element of the Array 4.Back Element of the Array 5.Display elements of the Array 6.Exit Enter your Choice: 2 Enter value to be inserted: 7 1.Size of the array 2.Insert Element into the Array 3.Front Element of the Array 4.Back Element of the Array 5.Display elements of the Array 6.Exit Enter your Choice: 2 Enter value to be inserted: 6 1.Size of the array 2.Insert Element into the Array 3.Front Element of the Array 4.Back Element of the Array 5.Display elements of the Array 6.Exit Enter your Choice: 2 Enter value to be inserted: 5 1.Size of the array 2.Insert Element into the Array 3.Front Element of the Array 4.Back Element of the Array 5.Display elements of the Array 6.Exit Enter your Choice: 2 Enter value to be inserted: 4 1.Size of the array 2.Insert Element into the Array 3.Front Element of the Array 4.Back Element of the Array 5.Display elements of the Array 6.Exit Enter your Choice: 2 Enter value to be inserted: 3 1.Size of the array 2.Insert Element into the Array 3.Front Element of the Array 4.Back Element of the Array 5.Display elements of the Array 6.Exit Enter your Choice: 2 Enter value to be inserted: 2 1.Size of the array 2.Insert Element into the Array 3.Front Element of the Array 4.Back Element of the Array 5.Display elements of the Array 6.Exit Enter your Choice: 2 Enter value to be inserted: 1 1.Size of the array 2.Insert Element into the Array 3.Front Element of the Array 4.Back Element of the Array 5.Display elements of the Array 6.Exit Enter your Choice: 3 Front Element of the Array: 7 1.Size of the array 2.Insert Element into the Array 3.Front Element of the Array 4.Back Element of the Array 5.Display elements of the Array 6.Exit Enter your Choice: 4 Back Element of the Stack: 1 1.Size of the array 2.Insert Element into the Array 3.Front Element of the Array 4.Back Element of the Array 5.Display elements of the Array 6.Exit Enter your Choice: 5 7 6 5 4 3 2 1 1.Size of the array 2.Insert Element into the Array 3.Front Element of the Array 4.Back Element of the Array 5.Display elements of the Array 6.Exit Enter your Choice: 6 exit status 1
- Related Articles
- C++ Program to Implement Deque in STL
- C++ Program to Implement Forward_List in STL
- C++ Program to Implement List in STL
- C++ Program to Implement Map in STL
- C++ Program to Implement Multimap in STL
- C++ Program to Implement Multiset in STL
- C++ Program to Implement Next_Permutation in STL
- C++ Program to Implement Pairs in STL
- C++ Program to Implement Prev_Permutataion in STL
- C++ Program to Implement Priority_queue in STL
- C++ Program to Implement Queue in STL
- C++ Program to Implement Set in STL
- C++ Program to Implement Set_Difference in STL
- C++ Program to Implement Set_Intersection in STL
- C++ Program to Implement Set_Symmetric_difference in STL

Advertisements