fork download
  1. #include <iostream>
  2. #include <map>
  3.  
  4. using namespace std;
  5. int main ()
  6. {
  7. map<char,int> MapFirst;
  8. map<char,int> MapSecond;
  9.  
  10. MapFirst['a']=1;
  11. MapFirst['b']=2;
  12.  
  13. MapSecond['z']=23;
  14. MapSecond['y']=22;
  15.  
  16. cout << "MapFirst keeps before swap: MapSecond keeps before swap:" << endl;
  17. auto itMapFirst=MapFirst.begin();
  18. auto itMapSecond=MapSecond.begin();
  19. for(itMapFirst,itMapSecond; itMapFirst != MapFirst.end(); ++itMapFirst,++itMapSecond)
  20. {
  21. cout << itMapFirst->first << " : " << itMapFirst->second << "\t\t\t\t\t"
  22. << itMapSecond->first << " : " << itMapSecond->second <<endl;
  23. }
  24.  
  25. swap(MapFirst,MapSecond);///обмен
  26.  
  27. cout << "MapFirst keeps after swap: MapSecond keeps after swap:" << endl;
  28. itMapFirst=MapFirst.begin();
  29. itMapSecond=MapSecond.begin();
  30. for(itMapFirst,itMapSecond; itMapFirst != MapFirst.end(); ++itMapFirst,++itMapSecond)
  31. {
  32. cout << itMapFirst->first << " : " << itMapFirst->second << "\t\t\t\t\t"
  33. << itMapSecond->first << " : " << itMapSecond->second <<endl;
  34. }
  35.  
  36. MapFirst.swap(MapSecond);///обмен
  37.  
  38. cout << "MapFirst keeps after anoter swap: MapSecond keeps after anoter swap:" << endl;
  39. itMapFirst=MapFirst.begin();
  40. itMapSecond=MapSecond.begin();
  41. for(itMapFirst,itMapSecond; itMapFirst != MapFirst.end(); ++itMapFirst,++itMapSecond)
  42. {
  43. cout << itMapFirst->first << " : " << itMapFirst->second << "\t\t\t\t\t"
  44. << itMapSecond->first << " : " << itMapSecond->second <<endl;
  45. }
  46.  
  47. return 0;
  48. }
Success #stdin #stdout 0s 4532KB
stdin
Standard input is empty
stdout
MapFirst keeps before swap:  MapSecond keeps before swap:
a : 1					y : 22
b : 2					z : 23
MapFirst keeps after swap:    MapSecond keeps after swap:
y : 22					a : 1
z : 23					b : 2
MapFirst keeps after anoter swap:    MapSecond keeps after anoter swap:
a : 1					y : 22
b : 2					z : 23