fork download
  1. #include <vector>
  2. #include <map> //for std::pair
  3. #include <iostream>
  4. #include <algorithm> //for std::rotate
  5.  
  6. typedef std::pair<int,int> entry;
  7. typedef std::vector<entry> storage;
  8.  
  9.  
  10. void print( const char* msg, const storage& obj )
  11. {
  12. std::cout<<msg<<std::endl;
  13. for(auto i : obj)
  14. {
  15. std::cout << i.first << "," << i.second << std::endl;
  16. }
  17. }
  18.  
  19. void lookup(int key, const storage& obj)
  20. {
  21. for(auto i : obj)
  22. {
  23. if( i.first == key )
  24. {
  25. std::cout<<"\t"<<key<<"=>"<<i.second<<std::endl;
  26. return;
  27. }
  28. }
  29. std::cout<<key<<"not found"<<std::endl;
  30. }
  31.  
  32. int main()
  33. {
  34. storage mymap = {entry(122,1),entry(12,2),entry(3,45)};
  35. print("Before rotation", mymap);
  36. lookup(12,mymap);
  37. std::rotate(mymap.begin(),mymap.begin()+1,mymap.end());
  38. print("After one rotation", mymap);
  39. lookup(12,mymap);
  40. std::rotate(mymap.begin(),mymap.begin()+1,mymap.end());
  41. print("After one more rotation", mymap);
  42. lookup(12,mymap);
  43. return 0;
  44. }
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
Before rotation
122,1
12,2
3,45
	12=>2
After one rotation
12,2
3,45
122,1
	12=>2
After one more rotation
3,45
122,1
12,2
	12=>2