zhi*_*fan 4 c++ set shared-ptr
我有类X:
class X {
public:
bool operator<(const SCN& other) const;
};
Run Code Online (Sandbox Code Playgroud)
我有以下代码
std::multiset<std::shared_ptr<X>> m;
Run Code Online (Sandbox Code Playgroud)
我的问题是:
对于这个m,如果我想从最小到最大的访问它的元素,可以使用以下代码吗?如果没有,怎么样?
for (auto& i : m) {
f(i);
}
Run Code Online (Sandbox Code Playgroud)你的套装是根据你的key_type订购的std::shared_ptr<X>.由于您std::shared_ptr<X>的可比性,std :: shared_ptr的顺序优先.
为了便于参考,multiset被定义为
template<
class Key,
class Compare = std::less<Key>,
class Allocator = std::allocator<Key>
> class multiset;
Run Code Online (Sandbox Code Playgroud)
可以看出,typename Compareis std::less<Key>和std :: less应该重载可能实现为的函数重载
constexpr bool operator()(const T &lhs, const T &rhs) const
{
return lhs < rhs;
}
Run Code Online (Sandbox Code Playgroud)
既lhs和rhs类型是T在这种情况下是Key这是我们多集实例与它的类型std::shared_ptr<X>.
| 归档时间: |
|
| 查看次数: |
3653 次 |
| 最近记录: |