Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why aren't these C++ STL unordered sets considered equal?

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
{   
}
like image 342
user1707438 Avatar asked Oct 06 '22 11:10

user1707438


1 Answers

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.

like image 181
James McNellis Avatar answered Oct 10 '22 03:10

James McNellis