您的位置:首页 > 编程语言 > C语言/C++

STL算法---集合算法

2014-08-28 17:36 162 查看
集合算法

set_union / set_intersection / set_difference / set_symmetric_difference

1. set_union

构造一个有序序列,包含两个序列中所有的不重复元素。

相当于[first1, last1)和[first2, last2)的并集.

重载版本使用自定义的比较操作

函数原形

template<class InIt1, class InIt2, class OutIt> OutIt set_union(InIt1 first1, InIt1 last1, InIt2 first2, InIt2 last2, OutIt x);

template<class InIt1, class InIt2, class OutIt, class Pred> OutIt set_union(InIt1 first1, InIt1 last1,InIt2 first2, InIt2 last2,OutIt x, Pred pr);

2. set_intersection

构造一个有序序列,其中元素在两个序列中都存在。

相当于[first1, last1)和[first2, last2)的交集.

重载版本使用自定义的比较操作

函数原形

template<class InIt1, class InIt2, class OutIt> OutIt set_intersection(InIt1 first1, InIt1 last1,InIt2 first2, InIt2 last2, OutIt x);

template<class InIt1, class InIt2, class OutIt, class Pred> OutIt set_intersection(InIt1 first1, InIt1 last1,InIt2 first2,InIt2 last2, OutIt x, Pred pr);

3. set_difference

构造一个有序序列,该序列仅保留第一个序列中存在的而第二个中不存在的元素。

相当于[first1, last1) - ([first1, last1)交[first2, last2))

重载版本使用自定义的比较操作

函数原形

template<class InIt1, class InIt2, class OutIt> OutIt set_difference(InIt1 first1, InIt1 last1,InIt2 first2, InIt2 last2, OutIt x);

template<class InIt1, class InIt2, class OutIt, class Pred> OutIt set_difference(InIt1 first1, InIt1 last1, InIt2 first2, InIt2 last2, OutIt x, Pred pr);

4. set_symmetric_difference

构造一个有序序列,该序列取两个序列的对称差集(并集-交集)

函数原形

template<class InIt1, class InIt2, class OutIt> OutIt set_symmetric_difference(InIt1 first1, InIt1 last1, InIt2 first2, InIt2 last2, OutIt x);

template<class InIt1, class InIt2, class OutIt, class Pred> OutIt set_symmetric_difference(InIt1 first1, InIt1 last1, InIt2 first2, InIt2 last2, OutIt x, Pred pr);

/////////////////////////////////////////////////////////////
#include "stdafx.h"
#include <algorithm>
#include <numeric>
#include <functional>
#include <vector>
#include <iostream>

int _tmain(int argc, _TCHAR* argv[])
{
std::vector<int> nV2, nV1;
std::vector<int>::iterator iter;

////////////////////////////////
// 升序
nV2.clear();
nV2.push_back(11);
nV2.push_back(14);
nV2.push_back(15);
nV2.push_back(15);
nV2.push_back(16);

// 升序
nV1.clear();
nV1.push_back(14);
nV1.push_back(15);
nV1.push_back(16);
nV1.push_back(17);

std::vector<int> nVResult(nV1.size() + nV2.size());	// 这里怎么计算结果的个数?
// 此时nVResult: 0, 0, 0, 0, 0, 0, 0, 0, 0

// 并集
iter = std::set_union(nV1.begin(), nV1.end(), nV2.begin(), nV2.end(), nVResult.begin());
// 此时nVResult: 11, 14, 15, 15, 16, 17, 0(iter指向这里), 0, 0
// 结果是: 11, 14, 15, 15, 16, 17

// 交集
iter = std::set_intersection(nV1.begin(), nV1.end(), nV2.begin(), nV2.end(), nVResult.begin());
// 此时nVResult: 14, 15, 16, 15(iter指向这里), 16, 17, 0, 0, 0
// 结果是: 14, 15, 16

// [first1, last1) - ([first1, last1)交[first2, last2))
iter = std::set_difference(nV1.begin(), nV1.end(), nV2.begin(), nV2.end(), nVResult.begin());
// 此时nVResult: 17, 15(iter指向这里), 16, 15, 16, 17, 0, 0, 0
// 结果是: 17

// (并集-交集)
iter = std::set_symmetric_difference(nV1.begin(), nV1.end(), nV2.begin(), nV2.end(), nVResult.begin());
// 此时nVResult: 11, 15, 17, 15(iter指向这里), 16, 17,0,0,0
// 结果是: 11, 15, 17

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  STL算法 集合算法 C++