Edit: I found out the debugger I was debugging with actually displayed the wrong 'current' breakpoint, showing me it was at the return line while it was really still in the loop. Sorry if I wasted anyone's time -.-
Cheers
Hi everyone,
I'm having some weird trouble I can't make anything out of. This is my function that gets the symmetrical difference of two vectors:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/** Returns the symmetric difference of given vectors.
* The symmetric difference contains all elements that are in a or b, but not in both a and b
*/
template <typename T>
static std::vector<T> SymmetricDifference(std::vector<T> const& a, std::vector<T> const& b) {
std::vector<T> result;
for (std::vector<T>::const_iterator it = a.cbegin(); it != a.cend(); ++it)
if (std::find(b.cbegin(), b.cend(), *it) == b.cend())
result.push_back(*it);
for (std::vector<T>::const_iterator it = b.cbegin(); it != b.cend(); ++it)
if (std::find(a.cbegin(), a.cend(), *it) == a.cend())
result.push_back(*it);
return result;
}
If I change the last for loop to this (using braces) it works correctly, but that doesn't make sense to me! It should be the same right!?
1 2 3 4
for (std::vector<T>::const_iterator it = b.cbegin(); it != b.cend(); ++it) {
if (std::find(a.cbegin(), a.cend(), *it) == a.cend())
result.push_back(*it);
}