相关疑难解决方法(0)

什么是透明比较器?

在C++ 14中,关联容器似乎已从C++ 11改变 - [associative.reqmts]/13说:

成员函数模板find,count,lower_bound,upper_bound,并且equal_range不得,除非类型参与重载决议Compare::is_transparent存在.

使比较器"透明"的目的是什么?

C++ 14还提供了这样的库模板:

template <class T = void> struct less {
    constexpr bool operator()(const T& x, const T& y) const;
    typedef T first_argument_type;
    typedef T second_argument_type;
    typedef bool result_type;
};

template <> struct less<void> {
    template <class T, class U> auto operator()(T&& t, U&& u) const
    -> decltype(std::forward<T>(t) < std::forward<U>(u));
    typedef *unspecified* is_transparent;
};
Run Code Online (Sandbox Code Playgroud)

因此,例如,std::set<T, std::less<T>>不会有一个透明的比较,而是std::set<T, std::less<>> …

c++ c++-faq c++14

101
推荐指数
4
解决办法
2万
查看次数

std::unordered_set 中的 std::string_view 和 std::string

假设您有一个std::unordered_set<std::string>.

您有一个std::string_view要在容器中搜索的对象。问题是,您不想std::string从您的 中创建 a std::string_view,因为这种首先违背了使用的目的std::string_view

不过,好像std::string_view应该可以作为key使用;应该有某种方式来比较std::string_viewand std::string,因为它们基本上代表同一件事。但是没有,无论如何都没有在 STL 中。

这是一个僵局,我是否被迫编写自己的比较对象std::string_viewstd::string与我的对象一起使用std::unordered_set

编辑:这个问题特定于 string_view 对象。“重复”问题不相关。正如预期的那样,我收到了一个独特问题的独特答案。

c++ stl unordered-set string-view c++17

10
推荐指数
1
解决办法
3516
查看次数

标签 统计

c++ ×2

c++-faq ×1

c++14 ×1

c++17 ×1

stl ×1

string-view ×1

unordered-set ×1