Set stores string objects - C++ STL

C++ examples for STL:set

Description

Set stores string objects

Demo Code

#include <iostream>
#include <set>
#include <string>
using namespace std;
int main()//from w w  w. j  av a2  s . co  m
{
   string names[] = {"A", "B","C", "D", "E"};
   set<string, less<string> > nameSet(names, names+5);
   set<string, less<string> >::iterator iter;
   nameSet.insert("Y");  // insert more names
   nameSet.insert("L");
   nameSet.insert("R");  // no effect; already in set
   nameSet.insert("B");
   nameSet.erase("B");
   cout << "\nSize=" << nameSet.size() << endl;
   iter = nameSet.begin();    // display members of set
   while( iter != nameSet.end() )
      cout << *iter++ << '\n';
   string searchName;         // get name from user
   cout << "\nEnter name to search for: ";
   cin >> searchName;
   // find matching name in set
   iter = nameSet.find(searchName);
   if( iter == nameSet.end() )
      cout << "The name " << searchName << " is NOT in the set.";
   else
      cout << "The name " << *iter << " IS in the set.";
   cout << endl;
   return 0;
}

Result


Related Tutorials