set_symmetric_difference原型:
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); |
该函数是计算两个集合的对称差。
使用前序列应该已经有序。
对称差的概念为:
其行为类似于:
template <class InputIterator1, class InputIterator2, class OutputIterator> OutputIterator set_symmetric_difference (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result) { while (true) { if (first1==last1) return std::copy(first2,last2,result); if (first2==last2) return std::copy(first1,last1,result); if (*first1<*first2) { *result=*first1; ++result; ++first1; } else if (*first2<*first1) { *result = *first2; ++result; ++first2; } else { ++first1; ++first2; } } }一个简单的例子:
#include <iostream> #include <algorithm> #include <vector> using namespace std; void setsydifference() { vector<int> vi{1,2,3,4,4,6,9,22,11}; vector<int> v2{1,2,3,5,4,9,8}; vector<int> vresult(10); sort(vi.begin(),vi.end()); sort(v2.begin(),v2.end()); cout<<"vi="; for(int i:vi) cout<<i<<" "; cout<<endl; cout<<"v2="; for(int i:v2) cout<<i<<" "; cout<<endl; auto it=set_symmetric_difference(vi.begin(),vi.end(),v2.begin(),v2.end(),vresult.begin()); cout<<"vreult="; for(auto i=vresult.begin();i!=it;++i) cout<<*i<<" "; cout<<endl; }运行截图:
set_union原型:
default (1) | template <class InputIterator1, class InputIterator2, class OutputIterator> OutputIterator set_union (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result); |
---|---|
custom (2) | template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare> OutputIterator set_union (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp); |
使用前序列应该已经有序,并且是升序。
其行为类似于:
template <class InputIterator1, class InputIterator2, class OutputIterator> OutputIterator set_union (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result) { while (true) { if (first1==last1) return std::copy(first2,last2,result); if (first2==last2) return std::copy(first1,last1,result); if (*first1<*first2) { *result = *first1; ++first1; } else if (*first2<*first1) { *result = *first2; ++first2; } else { *result = *first1; ++first1; ++first2; } ++result; } }一个简单的例子:
#include <iostream> #include <algorithm> #include <vector> using namespace std; void setunion() { vector<int> vi{1,2,3,4,4,6,9,22,11}; vector<int> v2{1,2,3,5,4,9,8}; vector<int> vresult(15); sort(vi.begin(),vi.end()); sort(v2.begin(),v2.end()); cout<<"vi="; for(int i:vi) cout<<i<<" "; cout<<endl; cout<<"v2="; for(int i:v2) cout<<i<<" "; cout<<endl; auto it=set_union(vi.begin(),vi.end(),v2.begin(),v2.end(),vresult.begin()); cout<<"vreult="; for(auto i=vresult.begin();i!=it;++i) cout<<*i<<" "; cout<<endl; }运行截图:
——————————————————————————————————————————————————————————————————
//写的错误或者不好的地方请多多指导,可以在下面留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。
转载请注明出处:http://blog.csdn.net/qq844352155
author:天下无双
Email:[email protected]
2014-9-26
于GDUT
——————————————————————————————————————————————————