c++ - 自 C++14 以来,总是更喜欢 set<T, less<>> 到 set<T>?

标签 c++ performance c++14 standards convention

#include <set>
#include <string>
#include <string_view>

using namespace std;

int main()
{
    string_view key = "hello";

    set<string> coll1;
    coll1.find(key); // error

    set<string, less<>> coll2;
    coll2.find(key); // ok since C++14
}

那么,它应该是一个规则:

总是喜欢 set<T, less<>> set<T> C++14 起

最佳答案

找到反例很简单:

#include <set>
#include <string>

using namespace std;

struct converts_to_string {
    operator string() const { return ""; }
};

int main()
{
    converts_to_string key;

    set<string> coll1;
    coll1.find(key); // OK

    set<string, less<>> coll2;
    coll2.find(key); // error
}

关于c++ - 自 C++14 以来,总是更喜欢 set<T, less<>> 到 set<T>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41094116/

相关文章:

c++ - 接受类型对象和所有派生类型对象的模板函数

c++ - 使用 floorf() 函数时无法链接 Embarcadero XE4 项目

c++ - 对 "same"类型转换的不同结果感到困惑, float 到 int

performance - 自动播放 YouTube 视频而不减慢页面加载时间

php - 从数据库表 MYSQL 中选择 2 个随机成员

c++ - 返回不同类型的局部变量被视为重载决策的右值

c++ - 如何沿任意轴缩放对象(在 3D 空间中)

C++继承不继承祖父文件

swift - TextView 在 Swift 中处理大量字符

c++ - 将返回类型推导为模板参数方法的类型