Ant*_*kov 4 c++ containers predicate associative
由于 C++14,我们std::less<void>在大多数情况下都是透明且更有用的,因此是否有原因,例如,默认情况下std::set仍将std::less<Key>其作为谓词,而不是std::less<void>历史原因除外。
有用的案例:std::set<std::string>::findwithstd::string_view等
这样做会破坏当前的工作代码。想象一下我有
struct my_type
{
int id;
int bar;
};
namespace std {
template<>
struct less<my_type>
{
bool operator()(my_type const& lhs, my_type const& rhs)
{
return lhs.id < rhs.id; // bar doesn't need to be compared, only need unique id's in the container.
}
};
}
std::set<my_type> foo;
Run Code Online (Sandbox Code Playgroud)
如果std::set更改为使用std::less<void>,则此代码将不再编译,因为my_type没有operator <.
| 归档时间: |
|
| 查看次数: |
168 次 |
| 最近记录: |