std::mismatch
| Defined in header <algorithm>
|
||
| template< class InputIterator1, class InputIterator2 > std::pair<InputIterator1,InputIterator2> |
(1) | |
| template< class InputIterator1, class InputIterator2, class BinaryPredicate > std::pair<InputIterator1,InputIterator2> |
(2) | |
Returns the first mismatching pair of elements from two ranges: one defined by [first1, last1) and another starting at first2. The first version of the function uses operator== to compare the elements, the second version uses the given binary predicate p.
目次 |
[編集] 引数
| first1, last1 | - | 1つめの比較される要素の範囲 | |||||||||
| first2 | - | 2つめの比較される要素の範囲の先頭 | |||||||||
| p | - | 要素が等しいと扱われるべき場合に true を返す2項述語. 述語のシグネチャは以下と等価である必要がある:
シグネチャは const & である必要はないが、この述語は渡されたオブジェクトを変更してはならない。 | |||||||||
[編集] 返値
最初の等価でない要素のイテレータから成る std::pair を返す。等価でない要素が見つからなかった場合、last1 と、対応する2つめの範囲のイテレータからなる std::pair を返す。
[編集] 計算量
At most last1 - first1 applications of the predicate
[編集] 等価コード
| First version |
|---|
template<class InputIterator1, class InputIterator2> std::pair<InputIterator1, InputIterator2> mismatch(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2) { for (; first1 != last1; ++first1, ++first2) { if (*first1 != *first2) { return std::make_pair(first1, first2); } } return std::make_pair(first1, first2); } |
| Second version |
template<class InputIterator1, class InputIterator2, class BinaryPredicate> std::pair<InputIterator1, InputIterator2> mismatch(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, BinaryPredicate p) { for (; first1 != last1; ++first1, ++first2) { if (!p(*first1, *first2)) { return std::make_pair(first1, first2); } } return std::make_pair(first1, first2); } |
[編集] 例
This program determines the the longest substring that is simultaneously found at the very beginning of the given string and at the very end of it, in reverse order (possibly overlapping)
#include <iostream> #include <string> #include <algorithm> std::string mirror_ends(const std::string& in) { return std::string(in.begin(), std::mismatch(in.begin(), in.end(), in.rbegin()).first); } int main() { std::cout << mirror_ends("abXYZba") << '\n' << mirror_ends("abca") << '\n' << mirror_ends("aba") << '\n'; }