簡介
set_union
set_intersection
set_difference
set_union取并集
構(gòu)造一個(gè)有序序列惧磺,包含兩個(gè)有序序列的并集颖对。
// TEMPLATE FUNCTION set_union
template<class _InIt1,
class _InIt2,
class _OutIt> inline
_OutIt set_union(_InIt1 _First1, _InIt1 _Last1,
_InIt2 _First2, _InIt2 _Last2, _OutIt _Dest)
{ // OR sets [_First1, _Last1) and [_First2, _Last2), using operator<
return (_STD set_union(_First1, _Last1, _First2, _Last2, _Dest,
less<>()));
}
#if _ITERATOR_DEBUG_ARRAY_OVERLOADS
template<class _InIt1,
class _InIt2,
class _OutTy,
size_t _OutSize> inline
_OutTy *set_union(_InIt1 _First1, _InIt1 _Last1,
_InIt2 _First2, _InIt2 _Last2, _OutTy (&_Dest)[_OutSize])
{ // OR sets [_First1, _Last1) and [_First2, _Last2), array dest
return (_STD set_union(_First1, _Last1, _First2, _Last2, _Dest,
less<>()));
}
#endif /* _ITERATOR_DEBUG_ARRAY_OVERLOADS */
set_intersection
構(gòu)造一個(gè)有序序列,包含兩個(gè)有序序列的交集磨隘。
// TEMPLATE FUNCTION set_intersection
template<class _InIt1,
class _InIt2,
class _OutIt> inline
_OutIt set_intersection(_InIt1 _First1, _InIt1 _Last1,
_InIt2 _First2, _InIt2 _Last2, _OutIt _Dest)
{ // AND sets [_First1, _Last1) and [_First2, _Last2), using operator<
return (_STD set_intersection(_First1, _Last1, _First2, _Last2, _Dest,
less<>()));
}
#if _ITERATOR_DEBUG_ARRAY_OVERLOADS
template<class _InIt1,
class _InIt2,
class _OutTy,
size_t _OutSize> inline
_OutTy *set_intersection(_InIt1 _First1, _InIt1 _Last1,
_InIt2 _First2, _InIt2 _Last2, _OutTy (&_Dest)[_OutSize])
{ // AND sets [_First1, _Last1) and [_First2, _Last2), array dest
return (_STD set_intersection(_First1, _Last1, _First2, _Last2, _Dest,
less<>()));
}
#endif /* _ITERATOR_DEBUG_ARRAY_OVERLOADS */
set_difference
構(gòu)造一個(gè)有序序列训堆,該序列保留第一個(gè)有序序列中存在而第二個(gè)有序序列中不存在的元素。
// TEMPLATE FUNCTION set_difference
template<class _InIt1,
class _InIt2,
class _OutIt> inline
_OutIt set_difference(_InIt1 _First1, _InIt1 _Last1,
_InIt2 _First2, _InIt2 _Last2,
_OutIt _Dest)
{ // take set [_First2, _Last2) from [_First1, _Last1), using operator<
return (_STD set_difference(_First1, _Last1, _First2, _Last2, _Dest,
less<>()));
}
#if _ITERATOR_DEBUG_ARRAY_OVERLOADS
template<class _InIt1,
class _InIt2,
class _OutTy,
size_t _OutSize> inline
_OutTy *set_difference(_InIt1 _First1, _InIt1 _Last1,
_InIt2 _First2, _InIt2 _Last2,
_OutTy (&_Dest)[_OutSize])
{ // take set [_First2, _Last2) from [_First1, _Last1), array dest
return (_STD set_difference(_First1, _Last1, _First2, _Last2, _Dest,
less<>()));
}
#endif /* _ITERATOR_DEBUG_ARRAY_OVERLOADS */
注意
- 集合必須有序
- 目的集合的大小要提前分配
- 自定義類型需要重載<操作符
示例
#include "stdafx.h"
#include "stdafx.h"
#include "iostream"
#include "string"
#include "algorithm"
#include "vector"
#include "list"
#include <functional>
#include<numeric>
using namespace std;
class Student {
private:
int number;
string name;
public:
Student() {
this->number = 0;
this->name = "";
}
Student(int number, string name) {
cout << "構(gòu)造 " << number << " " << name.c_str() << endl;
this->number = number;
this->name = name;
}
Student(const Student & stu) {
//cout << "copy構(gòu)造" <<stu.getNumber()<<" "<<stu.getName().c_str()<< endl;
this->number = stu.getNumber();
this->name = stu.getName();
}
~Student() {
//cout<<"析構(gòu) " << this->number << " " << this->name.c_str() << endl;
}
Student& operator=(const Student& stu) {
this->number = stu.getNumber();
this->name = stu.getName();
return *this;
}
void print()const {
cout << "print 》》 " << this->number << " " << this->name.c_str() << endl;
}
int getNumber() const {
return this->number;
}
string getName()const {
return this->name;
}
// 小于運(yùn)算符用于集合運(yùn)算
bool operator<(const Student &stu) const
{
return this->getNumber() < stu.getNumber();
}
};
void printStuV(vector<Student> v) {
cout << "開始遍歷vector<Student>============" << endl;
for (vector<Student>::iterator it = v.begin(); it != v.end(); it++) {
it->print();
}
cout << "結(jié)束遍歷vector<Student>============" << endl;
}
void printNum(vector<int>v) {
cout << "開始遍歷vector<int>============" << endl;
for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
cout << *it << " ";
}
cout << endl;
cout << "結(jié)束遍歷vector<int>============" << endl;
}
struct ReplaceFunc
{
bool operator()(const Student & stu1) const {
cout << "ReplaceFunc》》" << endl;
return stu1.getNumber() >3;
}
};
int main()
{
//accumulate示例允扇,需要include<numeric>
vector<int> vNum;
vNum.push_back(1);
vNum.push_back(2);
vNum.push_back(3);
vector<int> vNum2;
vNum2.push_back(2);
vNum2.push_back(3);
vNum2.push_back(5);
vector<int> vNum3;
vNum3.resize(10);
//取并集
set_union(vNum.begin(),vNum.end(),vNum2.begin(),vNum2.end(),vNum3.begin());
printNum(vNum3);
fill(vNum3.begin(), vNum3.end(), 0);
//交集
set_intersection(vNum.begin(), vNum.end(), vNum2.begin(), vNum2.end(), vNum3.begin());
printNum(vNum3);
fill(vNum3.begin(), vNum3.end(), 0);
//差集
set_difference(vNum.begin(), vNum.end(), vNum2.begin(), vNum2.end(), vNum3.begin());
printNum(vNum3);
fill(vNum3.begin(), vNum3.end(), 0);
cout << endl;
//將輸入值賦給標(biāo)志范圍內(nèi)的所有元素读处。
vector<Student> vStu;
vector<Student> vStu2;
vector<Student> vStu3;
vStu3.resize(10);
vStu.push_back(Student(1, "one"));
vStu.push_back(Student(2, "two"));
vStu.push_back(Student(3, "three"));
vStu2.push_back(Student(3, "three_2"));
vStu2.push_back(Student(4, "four"));
//需要重載<操作符
set_union(vStu.begin(), vStu.end(), vStu2.begin(), vStu2.end(), vStu3.begin());
printStuV(vStu3);
}
結(jié)果: