交换算法swap

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

class Print
{
public:
   void operator()(int i)
   {
      cout << i << endl;
   }
};

int main()
{
   vector<int> v1;
   vector<int> v2;
   for(int i = 0; i < 5; i++)
   {
      v1.push_back(i);
      v2.push_back(i + 50);
   }

   for_each(v1.begin(), v1.end(), Print());
   for_each(v2.begin(), v2.end(), Print());

   cout << "-------" << endl;

   swap(v1, v2);
   for_each(v1.begin(), v1.end(), Print());
   for_each(v2.begin(), v2.end(), Print());
   
   return 0;
}
$ ./a.out 
0
1
2
3
4
50
51
52
53
54
-------
50
51
52
53
54
0
1
2
3
4
posted @ 2022-07-31 16:30  thomas_blog  阅读(38)  评论(0编辑  收藏  举报