Question

I expected the two unordered sets below to be evaluated as equivalent, but to my surprise they are not. This occurs because the two strings are stored in the same hash bucket and the operator== does a sequential comparison for the items in the set. Should this be considered a bug in std::unordered_set? Does anyone have an elegant workaround for this?

std::unordered_set<std::string> a,b;
a.insert("500666");
a.insert("961021");
b.insert("961021");
b.insert("500666");

if (a == b)   // condition is evaulated as false
{   
}
Was it helpful?

Solution

This is a known bug in the Visual C++ 2010 Standard Library implementation. This bug has been fixed in Visual C++ 2012; if this bug is affecting you, it might be worth looking into upgrading. (There was a bug on Microsoft Connect, but it seems to have vanished; I'm trying to figure out what happened to it.)

As a workaround, consider whether you really need to use the unordered associative containers--their performance is not necessarily better than the performance of the ordered associative containers.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top