fork download
  1. #include <iostream>
  2. #include <set>
  3. #include <string>
  4.  
  5. typedef std::set<std::pair<std::string, std::string> > ssset;
  6.  
  7. ssset::iterator get_key(ssset& s, std::string key)
  8. {
  9. ssset::iterator it = s.lower_bound(std::make_pair(key, ""));
  10. if (it != s.end() && it->first == key) return it;
  11. return s.end();
  12. }
  13.  
  14. ssset::const_iterator get_key(const ssset& s, std::string key)
  15. {
  16. ssset::const_iterator it = s.lower_bound(std::make_pair(key, ""));
  17. if (it != s.end() && it->first == key) return it;
  18. return s.end();
  19. }
  20.  
  21. void insert(ssset& s, std::string key, std::string value)
  22. {
  23. s.insert(std::make_pair(key, value));
  24. }
  25.  
  26. int main() {
  27. ssset s;
  28. insert(s, "abc", "def");
  29. insert(s, "def", "def");
  30. insert(s, "abc", "ghi");
  31. insert(s, "abc", "ghi");
  32. insert(s, "abc", "abc");
  33.  
  34. std::cout << "abc" << std::endl;
  35. for (ssset::iterator it = get_key(s, "abc"); it != s.end() && it->first == "abc"; ++it)
  36. std::cout << it->first << "->" << it->second << std::endl;
  37.  
  38. std::cout << "def" << std::endl;
  39. for (ssset::iterator it = get_key(s, "def"); it != s.end() && it->first == "def"; ++it)
  40. std::cout << it->first << "->" << it->second << std::endl;
  41.  
  42. std::cout << "ghi" << std::endl;
  43. for (ssset::iterator it = get_key(s, "ghi"); it != s.end() && it->first == "ghi"; ++it)
  44. std::cout << it->first << "->" << it->second << std::endl;
  45.  
  46. std::cout << "----- ALL ----" << std::endl;
  47. for (ssset::iterator it = s.begin(); it != s.end(); ++it)
  48. std::cout << it->first << "->" << it->second << std::endl;
  49.  
  50. }
Success #stdin #stdout 0.01s 2820KB
stdin
Standard input is empty
stdout
abc
abc->abc
abc->def
abc->ghi
def
def->def
ghi
----- ALL ----
abc->abc
abc->def
abc->ghi
def->def