GeezerVison is showing cpprefjp.github.io — std::ranges::mismatch - cpprefjp C++日本語リファレンス
最終更新日時:
2026年09月11日 13時06分31秒 (JST)
Akira Takahashi
が更新
履歴
編集
function template
<algorithm>
std::ranges:: mismatch (C++20)
namespace std :: ranges {
template < input_iterator I1 ,
sentinel_for < I1 > S1 ,
input_iterator I2 ,
sentinel_for < I2 > S2 ,
class Pred = ranges::equal_to ,
class Proj1 = identity ,
class Proj2 = identity >
requires indirectly_comparable < I1 , I2 , Pred , Proj1 , Proj2 >
constexpr mismatch_result < I1 , I2 >
mismatch ( I1 first1 ,
S1 last1 ,
I2 first2 ,
S2 last2 ,
Pred pred = {},
Proj1 proj1 = {},
Proj2 proj2 = {}); // (1) C++20
template < input_range R1 ,
input_range R2 ,
class Pred = ranges::equal_to ,
class Proj1 = identity ,
class Proj2 = identity >
requires indirectly_comparable < iterator_t < R1 > , iterator_t < R2 > , Pred , Proj1 , Proj2 >
constexpr mismatch_result <
borrowed_iterator_t < R1 > ,
borrowed_iterator_t < R2 >
>
mismatch ( R1 && r1 ,
R2 && r2 ,
Pred pred = {},
Proj1 proj1 = {},
Proj2 proj2 = {}); // (2) C++20
template < execution-policy Ep ,
random_access_iterator I1 ,
sized_sentinel_for < I1 > S1 ,
random_access_iterator I2 ,
sized_sentinel_for < I2 > S2 ,
class Pred = ranges::equal_to ,
class Proj1 = identity ,
class Proj2 = identity >
requires indirectly_comparable < I1 , I2 , Pred , Proj1 , Proj2 >
mismatch_result < I1 , I2 >
mismatch ( Ep && exec ,
I1 first1 ,
S1 last1 ,
I2 first2 ,
S2 last2 ,
Pred pred = {},
Proj1 proj1 = {},
Proj2 proj2 = {}); // (3) C++26
template < execution-policy Ep ,
sized-random-access-range R1 ,
sized-random-access-range R2 ,
class Pred = ranges::equal_to ,
class Proj1 = identity ,
class Proj2 = identity >
requires indirectly_comparable < iterator_t < R1 > , iterator_t < R2 > , Pred , Proj1 , Proj2 >
mismatch_result <
borrowed_iterator_t < R1 > ,
borrowed_iterator_t < R2 >
>
mismatch ( Ep && exec ,
R1 && r1 ,
R2 && r2 ,
Pred pred = {},
Proj1 proj1 = {},
Proj2 proj2 = {}); // (4) C++26
}
概要
2つのシーケンスが一致していない場所を検索する。
(1): イテレータ範囲を指定する
(2): Rangeを直接指定する
(3): (1)の並列アルゴリズム版。実行ポリシーを指定する
(4): (2)の並列アルゴリズム版。実行ポリシーを指定する
戻り値
[first1,last1) 内にあるイテレータ i と、j == first2 + (i - first1) であるイテレータ j について、
jが範囲[first2, last2)に含まれており、
!(*i == *j) もしくは
invoke (pred, invoke (proj1, *i), invoke (proj2, *j)) == false であるような、最初のイテレータのペア mismatch_result { .in1 = i, .in2 = j } を返す。
そのようなイテレータが見つからなかった場合は mismatch_result { .in1 = last1, .in2 = first2 + (last1 - first1)} を返す。
計算量
最大で last1 - first1 回の対応する述語が適用される。
例
基本的な使い方
#include <algorithm>
#include <iostream>
#include <vector>
#include <array>
#include <list>
#include <iterator> // for begin, end
#include <string> // for to_string
// mismatch の結果で得られた pair に対する情報を出力する
template < class Range1 , class Range2 , class I1 , class I2 >
void print_mismatch_value ( const Range1 & r1 , const Range2 & r2 , const I1 & i1 , const I2 & i2 ) {
std::cout << "mismatch index: " << std :: ranges::distance ( std::begin ( r1 ), i1 ) << std::endl ;
std::cout << "mismatch value: (" << ( std :: ranges::end ( r1 ) == i1 ? "end" : std::to_string ( * i1 )) << ","
<< ( std :: ranges::end ( r2 ) == i2 ? "end" : std::to_string ( * i2 )) << ")"
<< std::endl ;
}
int main () {
const std::vector < int > v = { 1 , 2 , 3 , 4 , 3 , 2 };
const std::array < int , 6 > v2 = { 1 , 2 , 4 , 3 , 2 , 1 };
const std::list < int > v3 = { 1 , 2 , 3 , 4 , 3 , };
// v と v2 で異なる場所を探す
{
auto [ i1 , i2 ] = std::ranges::mismatch ( v , v2 );
print_mismatch_value ( v , v2 , i1 , i2 );
}
std::cout << std::endl ;
// v と v3 で異なる場所を探す。
{
auto [ i1 , i2 ] = std::ranges::mismatch ( v3 , v );
print_mismatch_value ( v3 , v , i1 , i2 );
}
}
出力
mismatch index: 2
mismatch value: (3,4)
mismatch index: 5
mismatch value: (end,2)
並列アルゴリズムの例 (C++26)
#include <algorithm>
#include <execution>
#include <iostream>
#include <vector>
int main () {
std::vector < int > v1 = { 1 , 2 , 3 , 4 , 5 };
std::vector < int > v2 = { 1 , 2 , 4 , 4 , 5 };
// 並列に2つのシーケンスが異なる場所を探す
auto [ it1 , it2 ] = std::ranges::mismatch ( std::execution::par , v1 , v2 );
if ( it1 == v1.end() ) {
std::cout << "all match" << std::endl ;
} else {
std::cout << "mismatch at index: " << std::distance ( v1.begin() , it1 ) << std::endl ;
std::cout << "values: " << * it1 << " vs " << * it2 << std::endl ;
}
}
出力
mismatch at index: 2
values: 3 vs 4
実装例
struct mismatch_impl {
template < input_iterator I1 , sentinel_for < I1 > S1 , input_iterator I2 , sentinel_for < I2 > S2 , class Pred = ranges::equal_to , class Proj1 = identity , class Proj2 = identity >
requires indirectly_comparable < I1 , I2 , Pred , Proj1 , Proj2 >
constexpr mismatch_result < I1 , I2 > operator ()( I1 first1 , S1 last1 , I2 first2 , S2 last2 , Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {}) const {
for ( ; first1 != last1 && first2 != last2 ; ++ first1 , ++ first2 )
if ( ! bool ( invoke ( pred , invoke ( proj1 , * first1 ), invoke ( proj2 , * first2 ))))
return { first1 , first2 };
return { first1 , first2 };
}
template < input_range R1 , input_range R2 , class Pred = ranges::equal_to , class Proj1 = identity , class Proj2 = identity >
requires indirectly_comparable < iterator_t < R1 > , iterator_t < R2 > , Pred , Proj1 , Proj2 >
constexpr mismatch_result < borrowed_iterator_t < R1 > , borrowed_iterator_t < R2 >> operator ()( R1 && r1 , R2 && r2 , Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {}) const {
return ( * this )( begin ( r1 ), end ( r1 ), begin ( r2 ), end ( r2 ), ref ( pred ), ref ( proj1 ), ref ( proj2 ));
}
};
inline constexpr mismatch_impl mismatch ;
バージョン
言語
処理系
参照