如何比较两个std :: maps与std :: weak_ptr作为键?

use*_*592 2 c++ comparison weak-ptr c++11

我有这样的代码:

#include <memory>
#include <map>

struct element {
  std::map<std::weak_ptr<int>, int> weights;
  bool operator<(const element &a) const { return this->weights < a.weights; }
};

int main() { return 0; }
Run Code Online (Sandbox Code Playgroud)

我想比较该类的两个实例,但是遇到编译器错误:

/usr/include/c++/4.8/bits/stl_pair.h: In instantiation of ‘constexpr bool std::operator<(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&) [with _T1 = const std::weak_ptr<int>; _T2 = int]’:
/usr/include/c++/4.8/bits/stl_pair.h:221:24: error: no match for ‘operator<’ (operand types are ‘const std::weak_ptr<int>’ and ‘const std::weak_ptr<int>’)
     { return __x.first < __y.first
/usr/include/c++/4.8/bits/stl_pair.h:222:23: error: no match for ‘operator<’ (operand types are ‘const std::weak_ptr<int>’ and ‘const std::weak_ptr<int>’)
       || (!(__y.first < __x.first) && __x.second < __y.second); }
/usr/include/c++/4.8/bits/stl_pair.h:222:65: error: body of constexpr function ‘constexpr bool std::operator<(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&) [with _T1 = const std::weak_ptr<int>; _T2 = int]’ not a return-statement
       || (!(__y.first < __x.first) && __x.second < __y.second); }
Run Code Online (Sandbox Code Playgroud)

看到no match for operator,我添加了以下代码,但没有帮助。

// from boost::weak_ptr
template<typename T, typename U>
bool operator<(const std::weak_ptr<T> &a, const std::weak_ptr<U> &b)
{
  return a.owner_before(b);
}
Run Code Online (Sandbox Code Playgroud)

当我尝试以下操作时,错误仍然存​​在:

  • 添加constexpr到任何此运算符;
  • 像这样向地图添加自定义比较器:std::map<std::weak_ptr<int>, int, std::owner_less<std::weak_ptr<int>>>

我可以通过以下方式编译此代码:

  1. 将运算符的返回语句替换为:return true;
  2. 将重量成员的类型更改为使用std::weak_ptr,例如std::map<int, int>;
  3. 向类元素添加自定义比较运算符,该运算符不比较地图,而是比较每个键和值。

选项1.和2.仅用于测试,并非选项;3.可能会出现,但我想了解为什么会出现此错误并在可能的情况下使用标准库。以我的理解,它应该编译:std::map具有一个operator<,用于比较内部树,应该与pairs<key, data>,用于比较,该树比较该对中的第一和第二个元素,如果提供operator<for,weak_ptr则至少应该起作用。

但这不起作用(至少不适用于g ++ 4.8。{1,2}),因此我的问题是:

  • 为什么它不起作用,为什么我会收到此错误消息?
  • 我如何可以比较两个maps具有weak_ptr关键?

更新,使用std::lexicographical_compare由KerrekSB建议。

我正在尝试比较两个不同的地图。在下面的示例中,映射m1和m2都具有相同的键,但是使用此键存储了不同的值。如果将这两个地图进行比较,则它们不应相等,一个应排在另一个之前。

#include <memory>
#include <map>
#include <iostream>

typedef std::owner_less<std::weak_ptr<int>> wp_less;
typedef std::map<std::weak_ptr<int>, int, wp_less> wp_map;
bool map_cmp(const wp_map &a, const wp_map &b)
{
  return std::lexicographical_compare(
    a.begin(), a.end(),
    b.begin(), b.end(),
    []( std::pair<std::weak_ptr<int> const, int> const & p, 
        std::pair<std::weak_ptr<int> const, int> const & q) -> bool 
      { return wp_less()(p.first, q.first); });
      //{ return wp_less()(p.first, q.first) 
      //    || ( ! (wp_less()(q.first, p.first)) && p.second < q.second); });
}

int main() 
{
  std::shared_ptr<int> sp_int(std::make_shared<int>(5));
  std::weak_ptr<int> wp_int(sp_int);
  wp_map m1, m2;
  m1[wp_int] = 1;
  m2[wp_int] = 2;
  std::cout << "m1 < m2=" << map_cmp(m1, m2) << "\nm2 < m1=" << map_cmp(m2, m1);
  return 0; 
}
Run Code Online (Sandbox Code Playgroud)

显示的输出表明两者相等:

m1 < m2=0
m2 < m1=0
Run Code Online (Sandbox Code Playgroud)

但是它们不是,通过使用注释的比较,结果变为:

m1 < m2=1
m2 < m1=0
Run Code Online (Sandbox Code Playgroud)

所以这给我留下了:

  • 要进行默认的字典比较,我该怎么做?
  • 从问题的原始部分来看,为什么会出现此错误,特别是是什么导致constexpr错误?

Ker*_* SB 5

请勿使用裸露的方法<,而应使用std::lexicographical_compare算法,您可以为其提供自定义谓词,例如std::owner_less

<操作符使用词典版本的比较与std::less谓词的默认版本,该版本不适用于弱指针。


这有点大话,所以让我为您举例说明一下:

std::map<std::weak_ptr<int>, int, std::owner_less<std::weak_ptr<int>>> a, b;

return std::lexicographical_compare(
    a.begin(), a.end(),
    b.begin(), b.end(),
    [](std::pair<std::weak_ptr<int> const, int> const & p,
       std::pair<std::weak_ptr<int> const, int> const & q)
    -> bool { return std::owner_less<std::weak_ptr<int>>()(p.first, q.first); });
Run Code Online (Sandbox Code Playgroud)

在此示例中,表达式a < b等同于:

std::lexicographical_compare(a.begin(), a.end(), b.begin(), b.end())
Run Code Online (Sandbox Code Playgroud)

这是行不通的,因为这将尝试比较线对,而线对又会尝试将弱指针与进行比较std::less<std::weak_ptr<int>>()。(问题当然是该算法使用迭代器,并且不知道各个容器中的比较器对象。通常,没有理由为什么两个具有相同值类型的映射甚至都应该使用相同的比较器。)

您可以根据需要编写类似的内容owner_before。这样做的好处std::owner_less是,它在同一次清洗中同时比较了弱指针共享指针。