std::set_symmetric_difference
来自cppreference.com
|
|
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
| 定义于头文件 <algorithm>
|
||
| template< class InputIt1, class InputIt2, class OutputIt > OutputIt set_symmetric_difference( InputIt1 first1, InputIt1 last1, |
(1) | |
| template< class InputIt1, class InputIt2, class OutputIt, class Compare > |
(2) | |
中未发现的排序范围
[first1, last1)和元素中未发现的范围内开始,[first2, last2)排序范围[first2, last2)从排序后的范围[first1, last1)从排序后的范围d_first的元素复制。由此获得的范围也进行排序。预计两个输入范围进行排序与operator<的第一个版本,第二个版本需要进行排序的比较函数comp。如果发现一些元件m次[first1, last1)和n[first2, last2)次,将被复制到d_first完全相同std::abs(m-n)倍。如果m>n,那么最后这些元素被复制m-n[first1,last1),否则最后n-m元素复制[first2,last2)。由此获得的范围不能重叠的输入范围.原文:
Copies the elements from the sorted range
[first1, last1) which are not found in the sorted range [first2, last2) and the elements from the sorted range [first2, last2) which are not found in the sorted range [first1, last1) to the range beginning at d_first. The resulting range is also sorted. The first version expects both input ranges to be sorted with operator<, the second version expects them to be sorted with the given comparison function comp. If some element is found m times in [first1, last1) and n times in [first2, last2), it will be copied to d_first exactly std::abs(m-n) times. If m>n, then the last m-n of those elements are copied from [first1,last1), otherwise the last n-m elements are copied from [first2,last2). The resulting range cannot overlap with either of the input ranges.目录 |
[编辑] 参数
| first1, last1 | - | |
| first2, last2 | - | |
| comp | - | 比较函数对象(即满足比较(Compare)要求的对象),若首个参数小于第二个,则返回 true 。比较函数的签名应等价于如下者: bool cmp(const Type1 &a, const Type2 &b); 签名不需要拥有 const & ,但函数对象必须不修改传递给它的对象。 |
| 类型要求 | ||
-InputIt1 必须满足 InputIterator 的要求。
| ||
-InputIt2 必须满足 InputIterator 的要求。
| ||
-OutputIt 必须满足 OutputIterator 的要求。
| ||
[编辑] 返回值
过去的结尾的Iterator构建范围.
[编辑] 复杂度
最2·(N1+N2-1)比较,其中N1= std::distance(first1, last1)和N2= std::distance(first2, last2).
原文:
At most 2·(N1+N2-1) comparisons, where N1 = std::distance(first1, last1) and N2 = std::distance(first2, last2).
[编辑] 可能的实现
| 版本一 |
|---|
template<class InputIt1, class InputIt2, class OutputIt> OutputIt set_difference(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, OutputIt d_first) { while (first1 != last1) { if (first2 == last2) return std::copy(first1, last1, d_first); if (*first1 < *first2) { *d_first++ = *first1++; } else { if (*first2 < *first1) { *d_first++ = *first2; } else { ++first1; } ++first2; } } return std::copy(first2, last2, d_first); } |
| 版本二 |
template<class InputIt1, class InputIt2, class OutputIt, class Compare> OutputIt set_difference(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, OutputIt d_first, Compare comp) { while (first1 != last1) { if (first2 == last2) return std::copy(first1, last1, d_first); if (comp(*first1, *first2)) { *d_first++ = *first1++; } else { if (comp(*first2, *first1)) { *d_first++ = *first2; } else { ++first1; } ++first2; } } return std::copy(first2, last2, d_first); } |
[编辑] 示例
运行此代码
#include <iostream> #include <vector> #include <algorithm> #include <iterator> int main() { std::vector<int> v1{1,2,3,4,5,6,7,8 }; std::vector<int> v2{ 5, 7, 9,10}; std::sort(v1.begin(), v1.end()); std::sort(v2.begin(), v2.end()); std::vector<int> v_intersection; std::set_symmetric_difference( v1.begin(), v1.end(), v2.begin(), v2.end(), std::back_inserter(v_intersection)); for(int n : v_intersection) std::cout << n << ' '; }
输出:
1 2 3 4 6 8 9 10
[编辑] 另请参阅
| 如果一个集合是另外一个集合的子集则返回true (函数模板) | |
| 计算两个集合的差集 (函数模板) | |
| 计算两个集合的并集 (函数模板) | |
| 计算两个集合的交集 (函数模板) |