C++ set get ranges
#include <iostream> #include <set> #include <string> using namespace std; int main()/*from ww w . ja va 2 s. 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; }