algorithm - STL C++

mismatch()

Declaration

template <class InputIterator1, InputIterator2>
pair mismatch(
	InputIterator1 xFirst1,
	InputIterator1 xLast1,
	InputIterator2 xFirst2
);

Description

This function returns a pair of iterators that point to the first element that does not match in the ranges from "xFirst1" up through the one before "xLast1" and the range starting at "xFirst2." The overloaded version uses the function "xComp" as a comparison function.

Header Include

#include <algorithm>

Overloads

template <class InputIterator1, InputIterator2, class BinaryPred>
pair mismatch(
	InputIterator1 xFirst1,
	InputIterator1 xLast1,
	InputIterator2 xFirst2,
	BinaryPred xComp
);

Example

#include <iostream>
#include <vector>
#include <algorithm>

int main()
{
	using namespace std;

	// Create two vector instances
	vector<char> qV1;
	qV1.push_back('X');
	qV1.push_back('o');
	qV1.push_back('a');
	qV1.push_back('X');
	qV1.push_back('.');
	qV1.push_back('n');
	qV1.push_back('e');
	qV1.push_back('t');

	vector<char> qV2(qV1);
	qV2[4] = 'I';

	vector<char>::iterator qIter;
	// Output the vectors
	cout << "V1: ";
	for (qIter = qV1.begin(); qIter != qV1.end(); ++qIter) {
		cout << *qIter;
	}
	cout << endl;
	cout << "V2: ";
	for (qIter = qV2.begin(); qIter != qV2.end(); ++qIter) {
		cout << *qIter;
	}
	cout << endl;

	typedef vector<char>::iterator TIter;
	pair<TIter, TIter> qMismatch;
	// Find the mismatched entries
	qMismatch = mismatch(qV1.begin( ), qV1.end( ), qV2.begin( ));

	cout << *(qMismatch.first) << " does not match "
		<< *(qMismatch.second) << endl;

	// Keep the window open
	cin.get();
	return 0;
}

Output

mismatch() Output
 

© 2007–2024 XoaX.net LLC. All rights reserved.