How to find the second element of two vectors that does not match in C++

1 Answer

0 votes
#include <iostream>
#include <vector>
#include <algorithm>

using std::cout;
using std::endl;
using std::vector;

bool compare(int x, int y) {
	return (x == y);
}

int main()
{
	vector<int> vec1{ 1, 2, 3, 4, 5, 6 };
	vector<int> vec2{ 1, 2, 7, 4, 9, 6 };
	std::pair<vector<int>::iterator, vector<int>::iterator> pr;

	pr = mismatch(vec1.begin(), vec1.end(), vec2.begin());

	cout << *pr.first << ' ' << *pr.second << endl;

	++pr.first; ++pr.second;

	pr = mismatch(pr.first, vec1.end(), pr.second, compare);

	cout << *pr.first << ' ' << *pr.second << endl;

	cout << endl;

	return 0;
}


/*
run:

3 7
5 9

*/

 



answered Apr 30, 2018 by avibootz
...