STLAlgorithmExtensions/MismatchAlgorithm

BOOST WIKI | STLAlgorithmExtensions | RecentChanges | Preferences | Page List | Links List

A version of the STL [mismatch] function that specifies both the start and end of the second range.

```template <class InputIterator1, class InputIterator2>
pair<InputIterator1, InputIterator2>
mismatch(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2);

template <class InputIterator1, class InputIterator2, class BinaryPredicate>
pair<InputIterator1, InputIterator2>
mismatch(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2,
BinaryPredicate binary_pred);
```

---

If you're looking for a way to only compare elements, for elements that exist in both containers (i.e. number of comparisons limited to the size of the smallest of the two containers), the following code will do. Keep in mind that if either iterator in the returned pair is set to its respective last, then there was no mismatch (i.e. Assuming result is the value returned, then: if (result.first!=last1 && result.second!=last2) /* a mismatch was found */ ):

```template <class InputIterator1, class InputIterator2>
pair<InputIterator1, InputIterator2>
mismatch(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2)
{
for (;first1!=last1 && first2!=last2;++first1,++first2)
if (!(*first1==*first2)) // Did it this way, since equality is more likely to be defined than inequality
break;

return make_pair(first1,first2);
}

template <class InputIterator1, class InputIterator2, class BinaryPredicate>
pair<InputIterator1, InputIterator2>
mismatch(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2,
BinaryPredicate binary_pred)
{
for (;first1!=last1 && first2!=last2;++first1,++first2)
if (!binary_pred(*first1,*first2))
break;

return make_pair(first1,first2);
}
```

---

Here is an alternative "better" solution that sets both iterators to their last values, if there is no mismatch. This makes testing after the fact more efficient, since the first iterator is always set to last when there is no mismatch, even if the first range is longer than the second (i.e. if (result.first!=last1) /* a mismatch was found */ )

```template <class InputIterator1, class InputIterator2>
pair<InputIterator1, InputIterator2>
mismatch(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2)
{
for (;first1!=last1 && first2!=last2;++first1,++first2)
if (!(*first1==*first2))
return make_pair(first1,first2);

return make_pair(last1,last2);
}

template <class InputIterator1, class InputIterator2, class BinaryPredicate>
pair<InputIterator1, InputIterator2>
mismatch(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2,
BinaryPredicate binary_pred)
{
for (;first1!=last1 && first2!=last2;++first1,++first2)
if (!binary_pred(*first1,*first2))
return make_pair(first1,first2);

return make_pair(last1,last2);
}
```

BOOST WIKI | STLAlgorithmExtensions | RecentChanges | Preferences | Page List | Links List
Edit text of this page | View other revisions
Last edited March 21, 2005 1:24 pm (diff)
Search:
Disclaimer: This site not officially maintained by Boost Developers