加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c set和shared_ptr

发布时间:2020-12-16 09:57:35 所属栏目:百科 来源:网络整理
导读:我有类X: class X { public: bool operator(const SCN other) const;}; 我有以下代码 std::multisetstd::shared_ptrX m; 我的问题是: 如何订购m中的数据? X(shared_ptr)或X.operator的地址 对于这个m,如果我想从最小到最大的访问它的元素,可以使用以下代
我有类X:

class X {
    public:
        bool operator<(const SCN& other) const;
};

我有以下代码

std::multiset<std::shared_ptr<X>> m;

我的问题是:

>如何订购m中的数据? X(shared_ptr)或X.operator的地址
>对于这个m,如果我想从最小到最大的访问它的元素,可以使用以下代码吗?如果没有,怎么样?

for (auto& i : m) {
    f(i);
}

解决方法

您的集合是根据您的key_type进行排序的,该密钥类型为std :: shared_ptr< X>.作为你的std :: shared_ptr< X>是 comparable,以 std::shared_ptr的顺序为准.

为了便于参考,multiset被定义为

template<
    class Key,class Compare = std::less<Key>,class Allocator = std::allocator<Key>
> class multiset;

可以看出,typename Compare是std :: less< Key>和std::less应该重载可能实现为的函数重载

constexpr bool operator()(const T &lhs,const T &rhs) const 
{
    return lhs < rhs;
}

lhs和rhs都是T类型,在本例中是Key,它是我们实例化多重集的类型,其中std :: shared_ptr< X>.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读