拥有typedef std::map<boost::<my_class>shared_ptr,my_description>如何从我的函数返回和shared_ptr?

Having typedef std::map<boost::shared_ptr<my_class>,my_description> how to return & shared_ptr from my function?

本文关键字:my ptr shared gt lt 我的 函数 返回 description class map      更新时间:2023-10-16

所以我有这样的函数:

boost::shared_ptr<my_class> get_class_by_name(std::string name)
{
    typedef std::map<boost::shared_ptr<my_class>, my_description> map_t;
    BOOST_FOREACH(map_t::value_type it, some_object.class_map)
    {
        my_description descr = it.second;
        if(descr.name == name)
        {
            return it.first;
        }
    }
    throw std::runtime_error("Class with such name was not found map not found!");
    boost::shared_ptr<my_class> null;
    return null;
}

我需要它返回这样的boost::shared_ptr,它将不是一个拷贝的ptr,但指针是内部的映射。我的主要目标是对result

执行这样的操作
boost::shared_ptr<my_class> result = get_class_by_name(name);
boost::shared_ptr<my_class> null;
result  =  null; //(or result.reset();)

,然后用其他ptr重新配置map中的指针。(我不需要删除对象,因为它可以在我清理map ptr时在其他线程中使用)

好吧,我不知道你到底想做什么,但这里有一个基本的想法:

typedef std::shared_ptr<my_class> my_class_ptr;
typedef std::map<my_class_ptr, my_description> my_map_t;
struct FindByName
{
  FindByName(cosnt std::string & s) : name(s) { };
  inline bool operator()(const my_description & d) { return name == d.name; }
private:
  std::string name;
};
/* Usage: */
my_map_t m = /* ... */
my_map_t::iterator it = std::find_if(m.begin(), m.end(), FindByName("bob"));
if (it != m.end()) m.erase(it);