C++ set stores string objects
#include <iostream> #include <set> #include <string> using namespace std; int main()// w w w . j a v a 2s. c o 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; }