how to compare two std::set?

前端 未结 4 1975
一向
一向 2021-02-03 18:45

I do such comparison of two std::set

#include 
#include 
using namespace std;

#include 
#include 

        
4条回答
  •  囚心锁ツ
    2021-02-03 19:30

    Yes, operator== is correctly defined for all standard containers (except the unordered containers - based on 23.2.5.2 of the standard), and will generally do a lexicographic comparison. See for example here. The relevant quote:

    Checks if the contents of lhs and rhs are equal, that is, whether lhs.size() == rhs.size() and each element in lhs has equivalent element in rhs at the same position.

    Since std::set is an ordered container, any set with the same size and same elements (given the comparators are the same) will necessarily have them in the same position, hence will compare equal.

提交回复
热议问题