std::map::operator==
假设您的地图 API 具有迭代器(或索引)、已排序、不包含重复项,并且还将其键和映射类型存储为嵌套 typedef,您可以及时实现相同的语义O(N)
:
#include <functional> // less
#include <algorithm> // includes
// O(N) complexity
template<class MyMap, class KeyCmp = std::less<typename MyMap::key_type, class TCmp = std::equal<typename MyMap::mapped_type> >
bool set_equality(MyMap const& lhs, MyMap const& rhs, KeyCmp keycmp, TCmp tcmp)
{
typedef typename MyMap::value_type Pair;
return
lhs.size() == rhs.size() &&
std::includes(
lhs.begin(), lhs.end(),
rhs.begin(), rhs.end(),
[](Pair const& p1, Pair const& p2){
return keycmp(p1.first, p2.first) && tcmp(p1.second, p2.second);
})
;
}