mismatch
2 スコープの要素をバイナリ述語によって指定される等値または同等の要素である意味で比較し、相違点が発生した最初の位置を検索します。
template<class InputIterator1, class InputIterator2>
pair<InputIterator1, InputIterator2> mismatch(
InputIterator1 _First1,
InputIterator1 _Last1,
InputIterator2 _First2
);
template<class InputIterator1, class InputIterator2, class BinaryPredicate>
pair<InputIterator1, InputIterator2> mismatch(
InputIterator1 _First1,
InputIterator1 _Last1,
InputIterator2 _First2
BinaryPredicate _Comp
);
パラメーター
_First1
テストされる最初の範囲内の先頭の要素の位置を示す入力反復子。_Last1
テストされる最初の範囲の最後の要素 1 を超える位置を示す入力反復子。_First2
テストされる 2 番目の範囲内の先頭の要素の位置を示す入力反復子。_Comp
2 個の要素が同じ値として取得する場合に満たされている必要条件を定義するユーザー定義の述語関数オブジェクト。バイナリ述語が満たされなかった場合に完了したら 2 個の引数を受け取り、true と false を返します。
戻り値
比較される範囲の要素に違いがないか、2 番目のバージョンのバイナリ述語がすべての要素によってコンテンツする最初のスコープの位置に 2 スコープの不一致は、最初の構成の反復子と 2 番目の範囲の位置に 2 番目の構成の反復子の位置を示す二つの反復子は 2 スコープ、最初の構成の反復子のポイントから最初の範囲と 2 番目の構成の反復子の最後の要素の一つ前の位置 1 に 2 番目のスコープでテストされた最後の要素を含む 1 を設定するペア。
解説
検索する範囲は有効である必要があります; すべてのポインターが dereferenceable なり、最後の位置は incrementation によって最初からアクセスできます。
アルゴリズムの時間の複雑さがスコープに含まれる要素の数で直線的です。
要素間の等価性を判断するために使用される operator== がオペランド間の等価関係を課さなければ必要があります。
使用例
// alg_mismatch.cpp
// compile with: /EHsc
#include <vector>
#include <list>
#include <algorithm>
#include <iostream>
// Return whether second element is twice the first
bool twice ( int elem1, int elem2 )
{
return elem1 * 2 == elem2;
}
int main( )
{
using namespace std;
vector <int> v1, v2;
list <int> L1;
vector <int>::iterator Iter1, Iter2;
list <int>::iterator L1_Iter, L1_inIter;
int i;
for ( i = 0 ; i <= 5 ; i++ )
{
v1.push_back( 5 * i );
}
int ii;
for ( ii = 0 ; ii <= 7 ; ii++ )
{
L1.push_back( 5 * ii );
}
int iii;
for ( iii = 0 ; iii <= 5 ; iii++ )
{
v2.push_back( 10 * iii );
}
cout << "Vector v1 = ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")" << endl;
cout << "List L1 = ( " ;
for ( L1_Iter = L1.begin( ) ; L1_Iter!= L1.end( ) ; L1_Iter++ )
cout << *L1_Iter << " ";
cout << ")" << endl;
cout << "Vector v2 = ( " ;
for ( Iter2 = v2.begin( ) ; Iter2 != v2.end( ) ; Iter2++ )
cout << *Iter2 << " ";
cout << ")" << endl;
// Testing v1 and L1 for mismatch under identity
pair<vector <int>::iterator, list <int>::iterator> results1;
results1 = mismatch (v1.begin( ), v1.end( ), L1.begin( ));
if ( results1.first == v1.end( ) )
cout << "The two ranges do not differ."
<< endl;
else
cout << "The first mismatch is between "
<< *results1.first << " & " << *results1.second
<< endl;
// Modifying L1
L1_inIter = L1.begin( );
L1_inIter++;
L1_inIter++;
L1.insert(L1_inIter, 100);
cout << "Modified L1 = ( " ;
for ( L1_Iter = L1.begin( ) ; L1_Iter!= L1.end( ) ; L1_Iter++ )
cout << *L1_Iter << " ";
cout << ")" << endl;
// Testing v1 with modified L1 for mismatch under identity
results1 = mismatch ( v1.begin( ), v1.end( ), L1.begin( ) );
if ( results1.first == v1.end( ) )
cout << "The two ranges do not differ."
<< endl;
else
cout << "The first mismatch is between "
<< *results1.first << " & " << *results1.second
<< endl;
// Test v1 and v2 for mismatch under the binary predicate twice
pair<vector <int>::iterator, vector <int>::iterator> results2;
results2 = mismatch ( v1.begin( ), v1.end( ), v2.begin( ), twice );
if ( results2.first == v1.end( ) )
cout << "The two ranges do not differ under the binary "
<< "predicate twice." << endl;
else
cout << "The first mismatch is between "
<< *results2.first << " & " << *results2.second
<< endl;
}
必要条件
ヘッダー: <algorithm>
名前空間: std