fork download
  1. #include <iostream>
  2. #include <map>
  3. #include <tuple>
  4. using namespace std;
  5.  
  6. int main() {
  7.  
  8. std::map<std::pair<int,int>, int> m{
  9. { {1,1}, 11 },
  10. { {1,2}, 12 },
  11. { {2,2}, 22 },
  12. { {2,4}, 24 },
  13. { {3,5}, 35 }
  14. };
  15.  
  16. cout << "Simple search: "<<std::endl;
  17. auto r1 = m.find ({1,2});
  18. if (r1!=m.end()) {
  19. std::cout<<"Found r1 -> "<< r1->second << std::endl;
  20. }
  21. else std::cout<<"r1 not found !"<<std::endl;
  22.  
  23. cout << "Range search: "<<std::endl;
  24. auto r2 = m.equal_range ({1,2});
  25. if (r2.first!=m.end()) {
  26. std::cout<<"Found range1 -> "<< r2.first->second << std::endl;
  27. }
  28. if (r2.second!=m.end()) {
  29. std::cout<<"Found range2 -> "<< r2.second->second << std::endl;
  30. }
  31. //attention the r2.first==r2.second if exact value not found.
  32.  
  33. cout << "Flexibilized range search: "<<std::endl;
  34. int x=2;
  35. auto r3 = m.equal_range ({x,0});
  36. for (auto r=r3.first; r!=m.end() && r->first.first==x; r++) {
  37. std::cout<<"In range -> "<< r->second << std::endl;
  38. }
  39.  
  40. return 0;
  41. }
Success #stdin #stdout 0s 4284KB
stdin
Standard input is empty
stdout
Simple search: 
Found r1 -> 12
Range search: 
Found range1 -> 12
Found range2 -> 22
Flexibilized range search: 
In range -> 22
In range -> 24