C++ set iterator

Description

C++ set iterator

#include <iostream>
#include <set>
#include <string>
using namespace std;
int main()/*  w ww . jav  a 2s  .  com*/
{
   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;
}
#include <iostream>
#include <set>
#include <string>
using namespace std;
int main()//from www .ja  v a  2s.  c o m
{                          // set of string objects
    set<string, less<string> > organic;
    // iterator to set
    set<string, less<string> >::iterator iter;
    organic.insert("C");  // insert organic compounds
    organic.insert("X");
    organic.insert("C");
    organic.insert("M");
    organic.insert("C");
    organic.insert("P");
    organic.insert("A");
    organic.insert("I");
    organic.insert("C");
    organic.insert("P");
    organic.insert("C");
    iter = organic.begin();    // display set
    while( iter != organic.end() )
       cout << *iter++ << '\n';
    string lower, upper;       // display entries in range
    cout << "\nEnter range (example C Czz): ";
    cin >> lower >> upper;
    iter = organic.lower_bound(lower);
    while( iter != organic.upper_bound(upper) )
       cout << *iter++ << '\n';
    return 0;
}



PreviousNext

Related