
- 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 Sorting containers in STL
In this C++ program, we implement Sorting containers in STL.
Functions and descriptions:
Functions used here: l.push_back() = It is used to push elements into a list from the front. l.sort() = Sorts the elements of the list. Where l is a list object.
Example Code
#include <iostream> #include <list> #include <string> #include <cstdlib> using namespace std; int main() { list<int> l; list<int>::iterator it; int c, i; while (1) { cout<<"1.Insert Element to the list"<<endl; cout<<"2.Display the List"<<endl; cout<<"3.Exit"<<endl; cout<<"Enter your Choice: "; cin>>c; switch(c) { case 1: cout<<"Enter value to be inserted"; cin>>i; l.push_back(i); break; case 2: l.sort(); cout<<"Elements of the List: "; for (it = l.begin(); it != l.end(); it++) cout<<*it<<" "; cout<<endl; break; case 3: exit(1); break; default: cout<<"Wrong Choice"<<endl; } } return 0; }
Output
1.Insert Element to the list 2.Display the List 3.Exit Enter your Choice: 1 Enter value to be inserted7 1.Insert Element to the list 2.Display the List 3.Exit Enter your Choice: 1 Enter value to be inserted10 1.Insert Element to the list 2.Display the List 3.Exit Enter your Choice: 1 Enter value to be inserted6 1.Insert Element to the list 2.Display the List 3.Exit Enter your Choice: 1 Enter value to be inserted4 1.Insert Element to the list 2.Display the List 3.Exit Enter your Choice: 2 Elements of the List: 4 6 7 10 1.Insert Element to the list 2.Display the List 3.Exit Enter your Choice: 3 Exit code: 1
- Related Articles
- Containers in C++ STL
- 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

Advertisements