Skip to content

Instantly share code, notes, and snippets.

@Experiment5X
Created May 7, 2014 21:17
Show Gist options
  • Save Experiment5X/b4323cff09e5fdac2034 to your computer and use it in GitHub Desktop.
Save Experiment5X/b4323cff09e5fdac2034 to your computer and use it in GitHub Desktop.
Example usage of std::mismatch algorithm.
#include <string>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(int argc, char *argv[])
{
vector<string> dogPeople = { "James", "Mary", "Tim", "Jane", "Greg" };
vector<string> catPeople = { "James", "Mary", "Lucy", "Roger", "Sam" };
// check for mismatches
pair<vector<string>::iterator, vector<string>::iterator> result =
std::mismatch(dogPeople.begin(), dogPeople.end(), catPeople.begin());
// analyze the result for a mismatch
if (result.first == dogPeople.begin() && dogPeople.at(0) == catPeople.at(0))
cout << "No mismatch" << endl;
else
cout << "Mismatch at " << (result.first - dogPeople.begin()) << ". " << *result.first << " != " << *result.second << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment