C++ 如何访问共享的ptr';使用find()查找数据

C++ 如何访问共享的ptr';使用find()查找数据,c++,reference,segmentation-fault,shared-ptr,smart-pointers,C++,Reference,Segmentation Fault,Shared Ptr,Smart Pointers,我有一个共享的指针。我想在地图中找到一个特定的元素,并将其提取出来。我想知道如何找出是否有我正在寻找的元素,以及如何找到该元素(如果存在)。我能得到这么多,但我不确定——评论中的问题:) 提前道歉,我是C++新手,我正在努力学习代码,以实现我想看到的……/P> 价格手册 typedef std::shared_ptr<std::map<std::string, std::shared_ptr<ExtendedPrice>>> ExtendedPriceMapP

我有一个共享的指针。我想在地图中找到一个特定的元素,并将其提取出来。我想知道如何找出是否有我正在寻找的元素,以及如何找到该元素(如果存在)。我能得到这么多,但我不确定——评论中的问题:)

提前道歉,我是C++新手,我正在努力学习代码,以实现我想看到的……/P> 价格手册

typedef std::shared_ptr<std::map<std::string, std::shared_ptr<ExtendedPrice>>> ExtendedPriceMapPtr;

class PriceManual : public Manual 
{
    public:
        PriceManual(const std::shared_ptr<Action> action_p, ExtendedPriceMapPtr extendedPriceMap_p);
        void print();
    private:
        ExtendedPriceMapPtr d_extendedPriceMap_p;
}
typedef std::shared_ptr ExtendedPriceMapPtr;
类别价格手册:公共手册
{
公众:
价格手册(const std::shared_ptr action_p,ExtendedPriceMapPtr extendedPriceMap_p);
作废打印();
私人:
ExtendedPriceMapPtr d_extendedPriceMap_p;
}
价格手册

PriceManual::PriceManual(const std::shared_ptr<Action> action_p, ExtendedPriceMapPtr extendedPriceMap_p) 
: Manual(action_p),
  d_extendedPriceMap_p(extendedPriceMap_p)

void ExtendedPriceMap::print(const Something& something) 
{
    if (d_extendedPriceMap_p->find("name") != d_extendedPriceMap_p->end())
    // Is this a correct way of finding an element and making sure it exists?
    {
    // I would like to do something like
    // std::shared_ptr<ExtendedPrice> ext_price_p = d_extendedPriceMap_p["name"];
    // const ExtendedPrice extended_price = ext_price_p->extended_price();
    // to eventually get that extended_price.
    }
}
PriceManual::PriceManual(const std::shared_ptr action_p,ExtendedPriceMapPtr extendedPriceMap_p)
:手动(操作p),
d_扩展价格图(扩展价格图)
void ExtendedPriceMap::print(const Something&Something)
{
如果(d_extendedPriceMap\u p->find(“name”)!=d_extendedPriceMap\u p->end())
//这是查找元素并确保其存在的正确方法吗?
{
//我想做一些像
//std::shared_ptr ext_price_p=d_extendedPriceMap_p[“name”];
//常量扩展价格扩展价格=扩展价格->扩展价格();
//最终得到那个延长的价格。
}
}

extended_price_map[some_key]之后是否有有效的对象?是否
const price&abc=extended_price->()
const Price abc=extended\u Price->Price()
?在这个问题上,你已经交替使用了这两个词。我可以发誓,我最近看到了类似的东西:。您应该关闭/删除至少一个问题。@user3152801哦,天哪。。。我想就是这样!我已经更新了我的代码,我有另一个问题哈哈,我更新了问题!谢谢您好,非常感谢您的回复和友好的解释。我确实查阅了更多关于CPPPreference的文档,并能够使其在我想要的原因中工作。:)