Add element to STL Ordered List - C++ STL

C++ examples for STL:list

Description

Add element to STL Ordered List

Demo Code

#include <iostream>
#include <list>
#include <string>
using namespace std;
int main()//  w  w  w.  j  a v  a  2  s  .  c o  m
{
   string s;
   list<string> LS;
   list<string>::iterator iter;
   while (true) {
      cout << "Enter string (ENTER to exit): ";
      getline(cin, s);
      if (s.size() == 0) {
         break;
      }
      LS.push_back(s);
   }
   LS.sort();                   // Sort, and then print elements.
   for (iter = LS.begin(); iter != LS.end(); iter++) {
      cout << *iter << endl;
   }
   return 0;
}

Result


Related Tutorials