是否有用于 std::set(或 std::map)的 STL 比较器,其带有提供基于值的查找的 shared_ptr 键?std::owner_less 究竟是做什么的?

Arj*_*non 5 c++ stl shared-ptr comparator c++11

我有一个std::mapshared_ptr<T>键,我需要它使用实际值(类型T,即*key)进行查找,而不是共享指针本身的值。

我知道我可以编写自己的自定义比较器(正如我在下面所做的那样),但我想知道 STL 是否专门为此目的提供了一个比较器。

为了演示我在说什么,我创建了这个使用 a std::setof 字符串的简单示例(我也将它放在 GitHub 上作为要点):

#include <set>
#include <string>
#include <memory>
#include <iostream>
#include <functional>

template< typename T >
struct shared_ptr_comparator {
  bool operator()(const std::shared_ptr<T> &a, const std::shared_ptr<T> &b) const {
    return std::less<T>()(*a, *b);
  }
};

void ptr_set_with_custom_comparator() {
    std::set< std::shared_ptr<std::string>, shared_ptr_comparator<std::string> > ptr_set;

    ptr_set.insert(std::make_shared<std::string>("world"));
    ptr_set.insert(std::make_shared<std::string>("hello"));
    ptr_set.insert(std::make_shared<std::string>("abc"));

    for(auto const& entry : ptr_set) {
        std::cout << *entry << std::endl;
    }
}

void ptr_set_with_owner_less() {
    std::set< std::shared_ptr<std::string>, std::owner_less<std::shared_ptr<std::string>> > ptr_set;

    ptr_set.insert(std::make_shared<std::string>("world"));
    ptr_set.insert(std::make_shared<std::string>("hello"));
    ptr_set.insert(std::make_shared<std::string>("abc"));

    for(auto const& entry : ptr_set) {
        std::cout << *entry << std::endl;
    }
}

void raw_set() {
    std::set<std::string> raw_set;

    raw_set.insert("world");
    raw_set.insert("hello");
    raw_set.insert("abc");

    for(auto const& entry : raw_set) {
        std::cout << entry << std::endl;
    }
}

int main() {
    std::cout << "A basic set of strings:" << std::endl;
    raw_set();
    std::cout << std::endl;

    std::cout << "A set of shared_ptr<string>s with owner_less as the comparator:" << std::endl;
    ptr_set_with_owner_less();
    std::cout << std::endl;

    std::cout << "A set of shared_ptr<string>s with the comparator shared_ptr_comparator:" << std::endl;
    ptr_set_with_custom_comparator();

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

上面的代码可以遵守clang++ -Wall -std=c++11。这是输出:

A basic set of strings:
abc
hello
world

A set of shared_ptr<string>s with owner_less as the comparator:
world
hello
abc

A set of shared_ptr<string>s with the comparator shared_ptr_comparator:
abc
hello
world
Run Code Online (Sandbox Code Playgroud)

在这里,迭代和打印内容时的排序std::set意味着正在比较 _actual 基础值)。对上述示例的快速概览:

  • 该函数raw_set仅使用set<string>(不使用shared_ptr),仅供参考。

  • 我可以用我的手写shared_ptr_comparator. ptr_set_with_custom_comparator利用它的功能按预期工作。

  • 该功能ptr_set_with_owner_less没有按预期工作。owner_less(或owner_before)是否依赖于指针本身的地址/值?

我有两个问题:

  • shared_ptr_comparatorSTL 中是否存在与(在上面的程序中定义)等价的东西?我问是因为我写的比较器似乎是一个非常常见的用例,如果 STL 没有任何等效的东西,我会感到非常惊讶。

  • owner_lessowner_before(它称之为)究竟是做什么的?他们是否只是检查底层指针的等效性?我不确定我是否正确使用它。

提前感谢您对此问题的任何回答。

Kam*_*rek 4

STL中是否存在与shared_ptr_comparator(在上面的程序中定义)等效的东西?我问这个问题是因为我编写的比较器似乎是一个非常常见的用例,如果 STL 没有与之等效的东西,我会感到非常惊讶。

我也很惊讶,但是不,STL 没有内置的指针比较器以这种方式工作。然而,有更好的方法来实现它,允许传递任何指针。

template<typename T, typename comp_t>
bool ptr_compare(T lhs, T rhs, comp_t comp) {
    return comp(*lhs, *rhs);
}
Run Code Online (Sandbox Code Playgroud)

你可以这样称呼它:

ptr_compare(a_ptr, b_ptr, std::less<int>())
Run Code Online (Sandbox Code Playgroud)

如果您想要一个与 STL 容器兼容的版本:

template<typename T>
bool ptr_less(T lhs, T rhs) {
    return std::less<decltype(*lhs)>()(*lhs, *rhs);
}
Run Code Online (Sandbox Code Playgroud)

owner_less和owner_before(它称之为)到底做了什么?他们只是检查底层指针的等价性吗?我不确定我是否正确使用它。

std::owner_less不按价值比较,而是按所有者比较,因此与您的问题无关。

std::shared_ptr<T>::owner_before被调用std::owner_less以查明排序。

  • @ArjunMenon [这里](http://stackoverflow.com/questions/21834131/stdshared-ptrowner-before-and-stdowner-less-what-exactly-is-meant-by-own)很好的解释。 (2认同)