#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
可以实现收缩内存的效果