C++ STL merge,set_union,set_intersection,set_difference,set_symmetric_difference
2014-04-05 17:40
302 查看
default (1) | template <class InputIterator1, class InputIterator2, class OutputIterator> OutputIterator set_difference (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result); |
---|---|
custom (2) | template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare> OutputIterator set_difference (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp); |
set_difference:只存在第一区间,不存在于第二区间的元素,如果原区间存在重复的元素,目标区间重复元素的个数是拿第一个区间的元素的个数减去第二区间的元素的个数,如果为负数,则目标区间对应重复元素的个数为0.
default (1) | template <class InputIterator1, class InputIterator2, class OutputIterator> OutputIterator set_intersection (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result); |
---|---|
custom (2) | template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare> OutputIterator set_intersection (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp); |
set_intersection:目标区间的元素不但存在于第一区间,同时也存在于第二区间,如果源区间存在重复元素,目标区间也会有重复元素,目标区间重复元素的个数是源区间内的重复个数较小的值。
default (1) | template <class InputIterator1, class InputIterator2, class OutputIterator> OutputIterator set_symmetric_difference (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result); |
---|---|
custom (2) | template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare> OutputIterator set_symmetric_difference (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp); |
set_symmetric_difference:目标元素区间内的元素存在于第一区间或存在于第二区间,但不同时存在于两个源区间;
如果源区间存在重复元素,目标区间也会有重复元素,目标区间重复元素的个数是两源区间内的重复个数差额。
#include "algostuff.hpp"
#include <iterator>
#include <ostream>
using namespace std;
int main(){
int array_1[] = {1,2,2,4,6,7,7,9};
int array_1_count = sizeof(array_1)/sizeof(int);
int array_2[] = {2,2,2,3,6,6,8,9};
int array_2_count = sizeof(array_2)/sizeof(int);
cout<<"array_1:";
copy(array_1,array_1+array_1_count,ostream_iterator<int>(cout," "));
cout<<endl;
cout<<"array_2:";
copy(array_2,array_2+array_2_count,ostream_iterator<int>(cout," "));
cout<<endl;
cout<<"merge():";
merge(array_1,array_1+array_1_count,array_2,array_2+array_2_count,ostream_iterator<int>(cout," "));
cout<<endl;
cout<<"set_union:";
set_union(array_1,array_1+array_1_count,array_2,array_2+array_2_count,ostream_iterator<int>(cout," "));
cout<<endl;
cout<<"set_intersection():";
set_intersection(array_1,array_1+array_1_count,array_2,array_2+array_2_count,ostream_iterator<int>(cout," "));
cout<<endl;
cout<<"set_difference():";
set_difference(array_1,array_1+array_1_count,array_2,array_2+array_2_count,ostream_iterator<int>(cout," "));
cout<<endl;
cout<<"set_symmetric_difference():";
set_symmetric_difference(array_1,array_1+array_1_count,array_2,array_2+array_2_count,ostream_iterator<int>(cout," "));
cout<<endl;
return 0;
}
编译后输出:
array_1:1 2 2 4 6 7 7 9
array_2:2 2 2 3 6 6 8 9
merge():1 2 2 2 2 2 3 4 6 6 6 7 7 8 9 9
set_union:1 2 2 2 3 4 6 6 7 7 8 9
set_intersection():2 2 6 9
set_difference():1 4 7 7
set_symmetric_difference():1 2 3 4 6 7 7 8
相关文章推荐
- 泛型算法系列35:set_difference()&&set_intersection()&&set_symmetric_difference()&&set_union()
- 几个STL算法:includes,set_difference、set_intersection、set_symmetric_difference、set_union, pre_permutation, next_permutation
- 关于C++中使用set_union、set_intersection、set_difference的总结
- C++集合运算函数总结-merge、set_union、set_difference、set_symmetric_difference、set_intersection
- STL之set_union、set_intersection、set_difference、set_symmetric_difference
- STL 之includes,set_intersection,set_union,set_difference,set_symmetric_difference
- STL 之includes,set_intersection,set_union,set_difference,set_symmetric_difference
- 几个STL算法:includes,set_difference、set_intersection、set_symmetric_difference、set_union, pre_permutation, next_permutation
- C++集合操作之集合对称差集:std::set_symmetric_difference
- 关于C++里面使用set_union,set_intersection等函数的使用总结
- 关于C++里面使用set_union,set_intersection等函数的使用总结
- C++ set_union,set_intersection,set_difference
- 关于C++里面使用set_union,set_intersection等函数的使用总结
- STL algorithm算法set_symmetric_difference,set_union(54)
- C++集合操作之集合并集:std::set_union
- STL源码剖析之算法:set_difference & set_symmetric_difference
- HDU2034 人见人爱A-B(C++STL--set_difference()的应用)
- 【python】集合操作set函数去重与集合、数组、元组中交集(intersection)、并集(union)、差集(difference)、对称差集(sysmmetric difference)
- C++:set_union is not a member of std;
- set_symmetric_difference