#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 < 10; i++)
{
v1.push_back(i);
v2.push_back(i + 5);
}
vector<int> vTarget;
vTarget.resize(v1.size());
vector<int>::iterator it =
set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());
for_each(vTarget.begin(), it, Print());
return 0;
}
$ ./a.out
5
6
7
8
9
注:
- 求交集的两个集合必须是有序序列
- 目标容器开辟空间,取两个容器较小值
- set_intersection返回值是交集中最后一个元素的位置