set_symmetric_difference
相并属于一个所有元素,但是,不是两者,排序的源区到单个,排序的目标范围,排序的标准可能是按二进制谓词指定。
template<class InputIterator1, class InputIterator2, class OutputIterator>
OutputIterator set_symmetric_difference(
InputIterator1 _First1,
InputIterator1 _Last1,
InputIterator2 _First2,
InputIterator2 _Last2,
OutputIterator _Result
);
template<class InputIterator1, class InputIterator2, class OutputIterator, class BinaryPredicate>
OutputIterator set_symmetric_difference(
InputIterator1 _First1,
InputIterator1 _Last1,
InputIterator2 _First2,
InputIterator2 _Last2,
OutputIterator _Result,
BinaryPredicate _Comp
);
参数
_First1
处理第一元素位置的输入迭代器在第一源区要相并排序和排序到表示两个源区的对称差分唯一的范围。_Last1
寻址个元素的输入迭代器位置的一源区要相并和排序顺序。第一到表示两个源区的对称差分唯一的范围。_First2
处理第一元素位置的输入迭代器位于第二两个连续的源区要相并和排序到表示两个源区的对称差分唯一的范围。_Last2
寻址个元素的输入迭代器位置的一个第二两个连续的源区要相并和排序到表示两个源区的对称差分唯一的范围。结果###_
解决的第一个元素的输出位置的迭代器在两个源区将相并到表示两个源区的对称差分的单个订单范围的目标范围。_Comp
用户定义的谓词函数对象定义一个元素大于另一个。 二进制谓词采用两个参数,并且在满足第一个元素小于第二个元素时返回 true,在未满足时返回 false。
返回值
寻址个元素的输出迭代器位置一个表示两个源区的对称差分的目标范围。
备注
引用的顺序源区必须是有效的;所有指针必须 dereferenceable,在每个序列中最后位置必须是可访问的。从开始递增。
目标范围不应重叠源区,并且应足够大以包含目标范围。
必须将每个源区的顺序,将指向 合并 算法的应用程序的一个前置条件。的顺序相同。将算法使用顺序组合的范围。
因为元素在范围内的相对顺序每个在目标范围,从而操作是稳定的。 算法将不修改源区。
输入迭代器的值类型是小于可比较进行排序,因此,将两个元素,可以决定为它们是等效的 (从这个意义上来讲没有其他比不小于) 或一个条件比其他条件更少。 这将导致在非等效元素之间进行排序。 当在两个源区时的等效元素,第一个范围的元素之前。第二个源范围的元素在目标范围。 如果源区包含元素的副本,则目标范围将包含这些元素出现在一个源区中超出这些元素发生在第二个源范围的数字的绝对值。
算法的复杂性是线性的最多 2 * ( (_Last1 – _First1) – (_Last2 – _First2) ) – 1 的非空源区的比较。
set_symmetric_difference 有两个相关的窗体:
有关这些功能如何运行的信息,请参阅 经过检查的迭代器。
示例
// alg_set_sym_diff.cpp
// compile with: /EHsc
#include <vector>
#include <algorithm>
#include <functional> // For greater<int>( )
#include <iostream>
// Return whether modulus of elem1 is less than modulus of elem2
bool mod_lesser (int elem1, int elem2 )
{
if ( elem1 < 0 )
elem1 = - elem1;
if ( elem2 < 0 )
elem2 = - elem2;
return elem1 < elem2;
}
int main( )
{
using namespace std;
vector <int> v1a, v1b, v1 ( 12 );
vector <int>::iterator Iter1a, Iter1b, Iter1, Result1;
// Constructing vectors v1a & v1b with default less-than ordering
int i;
for ( i = -1 ; i <= 4 ; i++ )
{
v1a.push_back( i );
}
int ii;
for ( ii =-3 ; ii <= 0 ; ii++ )
{
v1b.push_back( ii );
}
cout << "Original vector v1a with range sorted by the\n "
<< "binary predicate less than is v1a = ( " ;
for ( Iter1a = v1a.begin( ) ; Iter1a != v1a.end( ) ; Iter1a++ )
cout << *Iter1a << " ";
cout << ")." << endl;
cout << "Original vector v1b with range sorted by the\n "
<< "binary predicate less than is v1b = ( " ;
for ( Iter1b = v1b.begin ( ) ; Iter1b != v1b.end ( ) ; Iter1b++ )
cout << *Iter1b << " ";
cout << ")." << endl;
// Constructing vectors v2a & v2b with ranges sorted by greater
vector <int> v2a ( v1a ) , v2b ( v1b ) , v2 ( v1 );
vector <int>::iterator Iter2a, Iter2b, Iter2, Result2;
sort ( v2a.begin ( ) , v2a.end ( ) , greater<int> ( ) );
sort ( v2b.begin ( ) , v2b.end ( ) , greater<int> ( ) );
cout << "Original vector v2a with range sorted by the\n "
<< "binary predicate greater is v2a = ( " ;
for ( Iter2a = v2a.begin ( ) ; Iter2a != v2a.end ( ) ; Iter2a++ )
cout << *Iter2a << " ";
cout << ")." << endl;
cout << "Original vector v2b with range sorted by the\n "
<< "binary predicate greater is v2b = ( " ;
for ( Iter2b = v2b.begin ( ) ; Iter2b != v2b.end ( ) ; Iter2b++ )
cout << *Iter2b << " ";
cout << ")." << endl;
// Constructing vectors v3a & v3b with ranges sorted by mod_lesser
vector <int> v3a ( v1a ), v3b ( v1b ) , v3 ( v1 );
vector <int>::iterator Iter3a, Iter3b, Iter3, Result3;
sort ( v3a.begin ( ) , v3a.end ( ) , mod_lesser );
sort ( v3b.begin ( ) , v3b.end ( ) , mod_lesser );
cout << "Original vector v3a with range sorted by the\n "
<< "binary predicate mod_lesser is v3a = ( " ;
for ( Iter3a = v3a.begin ( ) ; Iter3a != v3a.end ( ) ; Iter3a++ )
cout << *Iter3a << " ";
cout << ")." << endl;
cout << "Original vector v3b with range sorted by the\n "
<< "binary predicate mod_lesser is v3b = ( " ;
for ( Iter3b = v3b.begin ( ) ; Iter3b != v3b.end ( ) ; Iter3b++ )
cout << *Iter3b << " ";
cout << ")." << endl;
// To combine into a symmetric difference in ascending
// order with the default binary predicate less <int> ( )
Result1 = set_symmetric_difference ( v1a.begin ( ) , v1a.end ( ) ,
v1b.begin ( ) , v1b.end ( ) , v1.begin ( ) );
cout << "Set_symmetric_difference of source ranges with default order,"
<< "\n vector v1mod = ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != Result1 ; Iter1++ )
cout << *Iter1 << " ";
cout << ")." << endl;
// To combine into a symmetric difference in descending
// order, specify binary predicate greater<int>( )
Result2 = set_symmetric_difference ( v2a.begin ( ) , v2a.end ( ) ,
v2b.begin ( ) , v2b.end ( ) ,v2.begin ( ) , greater <int> ( ) );
cout << "Set_symmetric_difference of source ranges with binary"
<< "predicate greater specified,\n vector v2mod = ( " ;
for ( Iter2 = v2.begin( ) ; Iter2 != Result2 ; Iter2++ )
cout << *Iter2 << " ";
cout << ")." << endl;
// To combine into a symmetric difference applying a user
// defined binary predicate mod_lesser
Result3 = set_symmetric_difference ( v3a.begin ( ) , v3a.end ( ) ,
v3b.begin ( ) , v3b.end ( ) , v3.begin ( ) , mod_lesser );
cout << "Set_symmetric_difference of source ranges with binary "
<< "predicate mod_lesser specified,\n vector v3mod = ( " ; ;
for ( Iter3 = v3.begin( ) ; Iter3 != Result3 ; Iter3++ )
cout << *Iter3 << " ";
cout << ")." << endl;
}
Output
Original vector v1a with range sorted by the
binary predicate less than is v1a = ( -1 0 1 2 3 4 ).
Original vector v1b with range sorted by the
binary predicate less than is v1b = ( -3 -2 -1 0 ).
Original vector v2a with range sorted by the
binary predicate greater is v2a = ( 4 3 2 1 0 -1 ).
Original vector v2b with range sorted by the
binary predicate greater is v2b = ( 0 -1 -2 -3 ).
Original vector v3a with range sorted by the
binary predicate mod_lesser is v3a = ( 0 -1 1 2 3 4 ).
Original vector v3b with range sorted by the
binary predicate mod_lesser is v3b = ( 0 -1 -2 -3 ).
Set_symmetric_difference of source ranges with default order,
vector v1mod = ( -3 -2 1 2 3 4 ).
Set_symmetric_difference of source ranges with binarypredicate greater specified,
vector v2mod = ( 4 3 2 1 -2 -3 ).
Set_symmetric_difference of source ranges with binary predicate mod_lesser specified,
vector v3mod = ( 1 4 ).
要求
标头: <算法>
命名空间: std