一、set_intersection
Function description:
- Find the intersection of two containers
Function prototype:
-
set_intersection(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);
// Find the intersection of two sets
// Note: The two sets must be an ordered sequence
// beg1 container 1 start iterator
// end1 container 1 end iterator
// beg2 container 2 start iterator
// end2 container 2 end iterator
// dest target container start iterator
Example:
#include <vector>
#include <algorithm>
class myPrint
{
public:
void operator()(int val)
{
cout << val << " ";
}
};
void test01()
{
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(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, myPrint());
cout << endl;
}
int main() {
test01();
system("pause");
return 0;
}
to sum up:
- The ordered sequence necessary for the intersection of two sets
- To open up space for the target container, you need to take a small value from the two containers
- The return value of set_intersection is the position of the last element in the intersection
二、set_union
Function description:
- Find the union of two sets
Function prototype:
-
set_union(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);
// Find the union of two sets
// Note: The two sets must be an ordered sequence
// beg1 container 1 start iterator
// end1 container 1 end iterator
// beg2 container 2 start iterator
// end2 container 2 end iterator
// dest target container start iterator
Example:
#include <vector>
#include <algorithm>
class myPrint
{
public:
void operator()(int val)
{
cout << val << " ";
}
};
void test01()
{
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() + v2.size());
//返回目标容器的最后一个元素的迭代器地址
vector<int>::iterator itEnd =
set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());
for_each(vTarget.begin(), itEnd, myPrint());
cout << endl;
}
int main() {
test01();
system("pause");
return 0;
}
to sum up:
- An ordered sequence necessary to find the union of two sets
- To open up space for the target container requires the addition of two containers
- The return value of set_union is the position of the last element in the union
三、set_difference
Function description:
- Find the difference of two sets
Function prototype:
-
set_difference(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);
// Find the difference of two sets
// Note: The two sets must be an ordered sequence
// beg1 container 1 start iterator
// end1 container 1 end iterator
// beg2 container 2 start iterator
// end2 container 2 end iterator
// dest target container start iterator
Example:
#include <vector>
#include <algorithm>
class myPrint
{
public:
void operator()(int val)
{
cout << val << " ";
}
};
void test01()
{
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( max(v1.size() , v2.size()));
//返回目标容器的最后一个元素的迭代器地址
cout << "v1与v2的差集为: " << endl;
vector<int>::iterator itEnd =
set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());
for_each(vTarget.begin(), itEnd, myPrint());
cout << endl;
cout << "v2与v1的差集为: " << endl;
itEnd = set_difference(v2.begin(), v2.end(), v1.begin(), v1.end(), vTarget.begin());
for_each(vTarget.begin(), itEnd, myPrint());
cout << endl;
}
int main() {
test01();
system("pause");
return 0;
}
to sum up:
- The ordered sequence necessary for the two sets of the difference set
- To open up space for the target container, the larger value needs to be taken from the two containers
- The return value of set_difference is the position of the last element in the difference set