Set operations on sorted structures 为STL中对两个有序序列的操作,包括包含、合并、求交集、求差、求对称差。
包含:如果[first2,last2)包含在[first1,last1)中,返回true,否则返回false。
bool includes(InputIterator1 first1, InputIterator1 last1,InputIterator2 first2, InputIterator2 last2)
bool includes(InputIterator1 first1, InputIterator1 last1,InputIterator2 first2, InputIterator2 last2, Compare comp)
合并:合并[first1,last1)与[first2,last2)两个集合。
OutputIterator set_union(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result)
OutputIterator set_union(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp)
求交集:求[first1,last1)与[first2,last2)两个集合的交集。
OutputIterator set_intersection(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result)
OutputIterator set_intersection(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp)
求差:求在[first1,last1)中,而不在[first2,last2)的元素集合。
OutputIterator set_difference(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result)
OutputIterator set_difference(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp)
求对称差:两个集合之和集 减去 两个集合之交集。
OutputIterator set_symmetric difference(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result)
OutputIterator set_symmetric difference(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp)
#include<iostream>
#include<algorithm>
#include<vector>
#include<iterator>
using namespace std;
void init(vector<int>& v, int a, int b)
{
v.clear();
for(int i = a; i <= b; i++){
v.push_back(i);
}
}
int main()
{
vector<int>v1, v2;
vector<int>::iterator itr;
cout << "Includes operator:" << endl;
init(v1,1,5);
init(v2,1,8);
cout << "Vector v1: ";
copy(v1.begin(),v1.end(),ostream_iterator<int>(cout," "));
cout << endl;
cout << "Vector v2: ";
copy(v2.begin(),v2.end(),ostream_iterator<int>(cout," "));
cout << endl;
cout << "v2 is in v1 ? " << (includes(v1.begin(),v1.end(),v2.begin(),v2.end()) ? "yes" : "no") << endl;
cout << "v1 is in v2 ? " << (includes(v2.begin(),v2.end(),v1.begin(),v1.end()) ? "yes" : "no") << endl;
cout << endl;
init(v1,1,5);
init(v2,3,8);
cout << "Vector v1: ";
copy(v1.begin(),v1.end(),ostream_iterator<int>(cout," "));
cout << endl;
cout << "Vector v2: ";
copy(v2.begin(),v2.end(),ostream_iterator<int>(cout," "));
cout << endl;
cout << "set_union operator on v1,v2: ";
set_union(v1.begin(),v1.end(),v2.begin(),v2.end(),ostream_iterator<int>(cout," "));
cout << endl << endl;
init(v1,1,5);
init(v2,3,8);
cout << "Vector v1: ";
copy(v1.begin(),v1.end(),ostream_iterator<int>(cout," "));
cout << endl;
cout << "Vector v2: ";
copy(v2.begin(),v2.end(),ostream_iterator<int>(cout," "));
cout << endl;
cout << "set_intersecion operator on v1,v2: ";
set_intersection(v1.begin(),v1.end(),v2.begin(),v2.end(),ostream_iterator<int>(cout," "));
cout << endl << endl;
init(v1,1,5);
init(v2,3,8);
cout << "Vector v1: ";
copy(v1.begin(),v1.end(),ostream_iterator<int>(cout," "));
cout << endl;
cout << "Vector v2: ";
copy(v2.begin(),v2.end(),ostream_iterator<int>(cout," "));
cout << endl;
cout << "set_difference operator, v1 to v2: ";
set_difference(v1.begin(),v1.end(),v2.begin(),v2.end(),ostream_iterator<int>(cout," "));
cout << endl;
cout << "set_difference operator, v2 to v1: ";
set_difference(v2.begin(),v2.end(),v1.begin(),v1.end(),ostream_iterator<int>(cout," "));
cout << endl << endl;
init(v1,1,5);
init(v2,3,8);
cout << "Vector v1: ";
copy(v1.begin(),v1.end(),ostream_iterator<int>(cout," "));
cout << endl;
cout << "Vector v2: ";
copy(v2.begin(),v2.end(),ostream_iterator<int>(cout," "));
cout << endl;
cout << "set_symmetric_difference operator, v1 to v2: ";
set_symmetric_difference(v1.begin(),v1.end(),v2.begin(),v2.end(),ostream_iterator<int>(cout," "));
cout << endl;
cout << "set_symmetric_difference operator, v2 to v1: ";
set_symmetric_difference(v2.begin(),v2.end(),v1.begin(),v1.end(),ostream_iterator<int>(cout," "));
cout << endl << endl;
return 0;
}
Includes operator:
Vector v1: 1 2 3 4 5
Vector v2: 1 2 3 4 5 6 7 8
v2 is in v1 ? no
v1 is in v2 ? yes
Vector v1: 1 2 3 4 5
Vector v2: 3 4 5 6 7 8
set_union operator on v1,v2: 1 2 3 4 5 6 7 8
Vector v1: 1 2 3 4 5
Vector v2: 3 4 5 6 7 8
set_intersecion operator on v1,v2: 3 4 5
Vector v1: 1 2 3 4 5
Vector v2: 3 4 5 6 7 8
set_difference operator, v1 to v2: 1 2
set_difference operator, v2 to v1: 6 7 8
Vector v1: 1 2 3 4 5
Vector v2: 3 4 5 6 7 8
set_symmetric_difference operator, v1 to v2: 1 2 6 7 8
set_symmetric_difference operator, v2 to v1: 1 2 6 7 8