算法简介:
函数原型:
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
//set_intersection——集合算法
void Print(int val)
{
cout << val << " ";
}
void test()
{
vector<int>v1;
vector<int>v2;
vector<int>vTarget;
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
v2.push_back(i + 5);
}
vTarget.resize(min(v1.size(), v2.size()));
vector<int>::iterator itEnd = set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());
for_each(vTarget.begin(), itEnd, Print);
cout << endl;
}
int main()
{
test();
system("pause");
return 0;
}
?注:
函数原型:
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
//set_union——集合算法
void Print(int val)
{
cout << val << " ";
}
void test()
{
vector<int>v1;
vector<int>v2;
vector<int>vTarget;
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
v2.push_back(i + 5);
}
vTarget.resize(v1.size() + v2.size());
vector<int>::iterator itEnd = set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());
for_each(vTarget.begin(), itEnd, Print);
cout << endl;
}
int main()
{
test();
system("pause");
return 0;
}
?注:
函数原型:
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
//set_difference——集合算法
void Print(int val)
{
cout << val << " ";
}
void test()
{
vector<int>v1;
vector<int>v2;
vector<int>vTarget;
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
v2.push_back(i + 5);
}
vTarget.resize(max(v1.size(), v2.size()));
//v1和v2的差集
vector<int>::iterator itEnd = set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());
for_each(vTarget.begin(), itEnd, Print);
cout << endl;
//v2和v1的差集
vector<int>::iterator itEnd2 = set_difference(v2.begin(), v2.end(), v1.begin(), v1.end(), vTarget.begin());
for_each(vTarget.begin(), itEnd2, Print);
cout << endl;
}
int main()
{
test();
system("pause");
return 0;
}
?注: