strange for loop behavior (?)

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;
	}


I'm calling the function like this:

1
2
3
4
static int const a[] = {1, 2, 3, 4, 5};
	static int const b[] = {0, 2, 3, 4, 6};

	VectorUtil::SymmetricDifference<int>(std::vector<int>(std::begin(a), std::end(a)), std::vector<int>(std::begin(b), std::end(b)));



Now I'm getting this result:

result = {1, 5, 0}

it should be

result = {1, 5, 0, 6}

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);
}


edit: using visual studio 2012
Last edited on
Topic archived. No new replies allowed.