Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to efficiently compare Sets in c++?

Tags:

c++

c++11

I want to compare two sets using the equal algorithm but it's giving me an error. How is it possible to know if two sets are equal or not?

if(equal (a.begin(), a.end(), v.begin(), v.end())
like image 966
Pavan Avatar asked Jun 07 '13 18:06

Pavan


People also ask

How do you compare sets?

Two sets A and B are equal if they have exactly the same elements. We write A = B. Two sets A and B are equivalent if n(A)= n(B). Another way of saying this is that two sets are equivalent if they have the same number of elements.

How can I compare two sets in C++?

The '==' is an operator in C++ STL performs equality comparison operation between two unordered sets and unordered_set::operator== is the corresponding operator function for the same. Parameters: This operator function takes reference of two unordered sets uset1 and uset2 as parameters which are to be compared.

How do you compare strings on a map?

The answer is quiet simple we can directly compare two maps by equivalence relational operator("=="), just like we compare two variables.


1 Answers

You can simply say a == v, or perhaps a.size() == v.size() && a == v. It's as efficient as it can be. (The latter form with the explicit size check may be better because set iterators aren't random-access.) Update: The size check is implied [thanks @juanchopanza!]

like image 125
Kerrek SB Avatar answered Oct 17 '22 01:10

Kerrek SB