fork download
  1. #include <iostream>
  2. #include <map>
  3. #include <string>
  4.  
  5. int main()
  6. {
  7. std::map< int, std::string > map { { 1, "one" }, { 3,"three" }, { 5,"five" },
  8. { 7, "seven" }, { 9, "nine" }, { 11, "eleven" } } ;
  9.  
  10. std::map<int,std::string>::key_type two = 2 ;
  11. std::map<int,std::string>::key_type ten = 10 ;
  12.  
  13. // iterate from two to ten inclusive
  14. // see: http://e...content-available-to-author-only...e.com/w/cpp/container/map/upper_bound
  15. // see: http://e...content-available-to-author-only...e.com/w/cpp/container/map/lower_bound
  16. auto end = map.upper_bound(ten) ;
  17. for( auto iter = map.lower_bound(two) ; iter != end ; ++iter )
  18. std::cout << '{' << iter->first << ',' << iter->second << "} " ;
  19. std::cout << '\n' ;
  20.  
  21. // iterate from two up to not including ten
  22. end = map.lower_bound(ten) ;
  23. for( auto iter = map.lower_bound(two) ; iter != end ; ++iter )
  24. std::cout << '{' << iter->first << ',' << iter->second << "} " ;
  25. std::cout << '\n' ;
  26.  
  27. map[2] = "two" ;
  28. map[10] = "ten" ;
  29.  
  30. // iterate from two to ten inclusive
  31. end = map.upper_bound(ten) ;
  32. for( auto iter = map.lower_bound(two) ; iter != end ; ++iter )
  33. std::cout << '{' << iter->first << ',' << iter->second << "} " ;
  34. std::cout << '\n' ;
  35.  
  36. // iterate from two up to not including ten
  37. end = map.lower_bound(ten) ;
  38. for( auto iter = map.lower_bound(two) ; iter != end ; ++iter )
  39. std::cout << '{' << iter->first << ',' << iter->second << "} " ;
  40. std::cout << '\n' ;
  41. }
  42.  
Success #stdin #stdout 0s 3036KB
stdin
Standard input is empty
stdout
{3,three} {5,five} {7,seven} {9,nine} 
{3,three} {5,five} {7,seven} {9,nine} 
{2,two} {3,three} {5,five} {7,seven} {9,nine} {10,ten} 
{2,two} {3,three} {5,five} {7,seven} {9,nine}