Apache C++ Standard Library Reference Guide

## mismatch()

Library:  Algorithms

`Function`

No Entries

### Summary

Algorithm that compares elements from two sequences and returns a pair of iterators pointing to the first two elements that don't match each other

### Synopsis

```#include <algorithm>

namespace std {
template <class InputIterator1, class InputIterator2>
pair<InputIterator1,InputIterator2>
mismatch(InputIterator1 start1, InputIterator1 finish1,
InputIterator2 start2);

template <class InputIterator1, class InputIterator2,
class BinaryPredicate>
pair<InputIterator1, Inputiterator2>
mismatch(InputIterator1 start1, InputIterator1 finish1,
InputIterator2 start2,
BinaryPredicate binary_pred);
}
```

### Description

The mismatch() algorithm compares members of two sequences and returns two iterators (i and j) that point to the first location in each sequence where the sequences differ from each other. mismatch() assumes that the second sequence has at least as many members as the first sequence. If the two sequences are identical, mismatch() returns a pair of iterators that point to the end of the first sequence and the corresponding location at which the comparison stopped in the second sequence.

The first version of mismatch() checks members of a sequence for equality, while the second version lets you specify a comparison function object. The comparison function object must be a binary predicate.

The iterators i and j returned by mismatch() are defined as follows:

```j  == start2  +  (i  -  start1)
```

and i is the first iterator in the range [start1, finish1) for which the appropriate one of the following conditions hold:

```!(*i  ==  *(start2  +  (i  -  start1)))
```

or

```binary_pred(*i, *(start2 + (i - start1))) == false
```

If all of the members in the two sequences match, mismatch() returns a pair of finish1 and start2 + (finish1 - start1).

### Complexity

At most finish1 - start1 applications of the corresponding predicate are done.

### Example

```//
//  mismatch.cpp
//

#include <algorithm>    // mismatch
#include <functional>   // less_equal
#include <iostream>     // cout, endl
#include <utility>      // pair
#include <vector>       // vector

int main ()
{
typedef std::vector<int, std::allocator<int> > Vector;
typedef Vector::const_iterator                 Iter;

const Vector::value_type d1[] = { 1, 2, 3, 4 };
const Vector::value_type d2[] = { 1, 3, 2, 4 };

// Set up two vectors.
const Vector vi1 (d1 + 0, d1 + sizeof d1 / sizeof *d1);
const Vector vi2 (d2 + 0, d2 + sizeof d2 / sizeof *d2);

// p1 will contain two iterators that point to
// the first pair of elements that are different
// between the two vectors.
std::pair<Iter, Iter> p1 =
std::mismatch (vi1.begin (), vi1.end (), vi2.begin ());

// Find the first two elements such that an element in
// the first vector is greater than the element in
// the second vector.
std::pair<Iter, Iter> p2 =
std::mismatch (vi1.begin (), vi1.end (),
vi2.begin (), std::less_equal<int>());

// Output results.
std::cout << *p1.first << ", " << *p1.second << std::endl;
std::cout << *p2.first << ", " << *p2.second << std::endl;

return 0;
}

Program Output:
```
```2, 3
3, 2
```

### Standards Conformance

ISO/IEC 14882:1998 -- International Standard for Information Systems -- Programming Language C++, Section 25.1.7