set_difference
結合屬於已排序的來源範圍中的所有項目,但是,若為第二個排序的來源範圍,結合成單一,排序的目的範圍,排序準則可能是由二進位述詞指定。
template<class InputIterator1, class InputIterator2, class OutputIterator>
OutputIterator set_difference(
InputIterator1 first1,
InputIterator1 last1,
InputIterator2 first2,
InputIterator2 last2,
OutputIterator result
);
template<class InputIterator1, class InputIterator2, class OutputIterator, class BinaryPredicate>
OutputIterator set_difference(
InputIterator1 first1,
InputIterator1 last1,
InputIterator2 first2,
InputIterator2 last2,
OutputIterator result,
BinaryPredicate comp
);
first1
處理輸入的 Iterator 第一個項目的位置在第一個排序的來源範圍會結合和排序到代表兩個來源範圍的差異的單一範圍。last1
處理輸入的 Iterator 超過最後一個項目的位置是在第一個排序的來源範圍會結合和排序到代表兩個來源範圍的差異的單一範圍。first2
處理輸入的 Iterator 第一個項目的位置在第二個兩個連續的已排序的來源範圍會結合和排序到代表兩個來源範圍的差異的單一範圍。last2
處理輸入的 Iterator 超過最後一個項目的位置是在第二個兩個連續的已排序的來源範圍會結合和排序到代表兩個來源範圍的差異的單一範圍。result
解決輸出 Iterator 的第一個項目的位置在來源範圍要結合至表示兩個來源範圍的差異的單一排序範圍內的目的範圍。comp
定義感受遠遠之使用者定義的述詞函式物件哪個項目大於另一個執行個體。 這個二進位述詞會採用兩個引數,而且應傳回 true ,當第一個項目小於則為第二個項目和 false 小於時。
解決輸出的 Iterator 超過最後一個項目的位置是在表示兩個來源範圍的差異的已排序之目的範圍。
參考的已排序資料來源範圍必須是有效的,任何指標必須 dereferenceable,而且每一個序列中最後一個位置必須是可取得的開頭會增加。
目的範圍不能重疊來源範圍,而且應該足以包含第一個來源範圍。
必須將每個已排序的來源範圍時,這個 set_difference 演算法的應用程式的前提是與排程相同符合與將演算法使用排序合併的範圍。
,其項目相對順序在每個範圍內的目的範圍時,具有儲存作業是否穩定。 演算法合併未修改來源範圍。
輸入 Iterator 的實值型別必須小於可比較的已排序,因此,將兩個項目,可以判斷或其相等 (因為都比其他不小於) 或是小於另一個。 這會導致排程在非對等的項目之間。 當有兩個來源範圍時的對等項目,在第一個範圍的項目在第二個原始範圍的項目之前在目的範圍。 如果來源範圍中包含項目的重複這類詳細在第一個來源範圍比第二個中,則目的範圍將包含這些項目出現在第一個來源範圍的超過這些項目出現在第二個原始範圍的數字。
演算法的複雜度是線性與最多 2 個* ((last1 – first1) (last2 – first2) – 1 非空白的來源範圍比較。
set_difference 有兩個關聯的表單:
如需這些函式如何運作的詳細資訊,請參閱 檢查過的 Iterator。
// alg_set_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 difference in asscending
// order with the default binary predicate less <int> ( )
Result1 = set_difference ( v1a.begin ( ) , v1a.end ( ) ,
v1b.begin ( ) , v1b.end ( ) , v1.begin ( ) );
cout << "Set_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 difference in descending
// order specify binary predicate greater<int>( )
Result2 = set_difference ( v2a.begin ( ) , v2a.end ( ) ,
v2b.begin ( ) , v2b.end ( ) ,v2.begin ( ) , greater <int> ( ) );
cout << "Set_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 difference applying a user
// defined binary predicate mod_lesser
Result3 = set_difference ( v3a.begin ( ) , v3a.end ( ) ,
v3b.begin ( ) , v3b.end ( ) , v3.begin ( ) , mod_lesser );
cout << "Set_difference of source ranges with binary "
<< "predicate mod_lesser specified,\n vector v3mod = ( " ; ;
for ( Iter3 = v3.begin( ) ; Iter3 != Result3 ; Iter3++ )
cout << *Iter3 << " ";
cout << ")." << endl;
}
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_difference of source ranges with default order,
vector v1mod = ( 1 2 3 4 ).
Set_difference of source ranges with binarypredicate greater specified,
vector v2mod = ( 4 3 2 1 ).
Set_difference of source ranges with binary predicate mod_lesser specified,
vector v3mod = ( 1 4 ).
標題: <algorithm>
命名空間: std