其他分享
首页 > 其他分享> > vector容器互换(swap)

vector容器互换(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;
   v1.push_back(1);
   v1.push_back(2);

   vector<int> v2;
   v2.push_back(10);
   v2.push_back(20);

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

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

   return 0;
}
$ ./a.out 
1
2
10
20
10
20
1
2

标签:begin,end,back,v1,v2,vector,swap,互换,Print
来源: https://www.cnblogs.com/zhangxuechao/p/16525754.html