When I'm using the == or != operator to compare two sets, does that operator actually compare the size of the two sets first? I'm wondering if I need to manually compare the two sizes first to make it more efficient, or if I would actually be making it less efficient. I know the equality and inequality operators will check size, I just don't know if it will do so first.
bool checkEqualTo( const set<int> & set1, const set<int> & set2 )
{
// Should I include comparison of sizes first?
if ( set1.size() != set2.size() )
{
return false;
}
if ( set1 != set2 )
{
return false;
}
return true;
}
Yes, that's the first thing that's checked — from the C++11 standard, §23.2.1 table 96 (Container requirements):
Expression:
a == b
(wherea
andb
denote values of typeX
andX
denotes a container class containing objects of typeT
)Operational semantics:
distance(a.begin(), a.end()) == distance(b.begin(), b.end()) && equal(a.begin(), a.end(), b.begin())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With