Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/162.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 具有typedef std::map<;boost::共享\u ptr<;我的班级>;,我的描述>;如何返回&;从我的功能中共享\u ptr?_C++_Boost_Shared Ptr - Fatal编程技术网

C++ 具有typedef std::map<;boost::共享\u ptr<;我的班级>;,我的描述>;如何返回&;从我的功能中共享\u ptr?

C++ 具有typedef std::map<;boost::共享\u ptr<;我的班级>;,我的描述>;如何返回&;从我的功能中共享\u ptr?,c++,boost,shared-ptr,C++,Boost,Shared Ptr,所以我有这样的功能: 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_de

所以我有这样的功能:

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\u ptr按名称获取类(std::string name)
{
typedef std::map\u t;
BOOST\u FOREACH(map\u t::value\u键入它,some\u object.class\u map)
{
my_description descr=it.second;
如果(descr.name==名称)
{
先把它还给我;
}
}
抛出std::runtime_错误(“未找到具有此名称的类映射未找到!”);
boost::shared_ptr null;
返回null;
}
我需要它返回这样的boost::shared\u ptr,它将不是ptr的副本,而是映射内部的指针。我的主要目标是做这样的事情,并取得好的效果

boost::shared_ptr<my_class> result = get_class_by_name(name);
boost::shared_ptr<my_class> null;
result  =  null; //(or result.reset();)
boost::shared_ptr result=get_class_by_name(name);
boost::shared_ptr null;
结果=空//(或result.reset();)

然后在地图上用其他ptr重新设计指针。(我不需要删除该对象,因为在清理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);
typedef std::shared_ptr my_class_ptr;
typedef std::映射我的映射;
结构FindByName
{
FindByName(cosnt std::string&s):名称{};
内联布尔运算符()(const my_description&d){return name==d.name;}
私人:
std::字符串名;
};
/*用法:*/
我的地图\u t m=/**/
my_map_t::iterator it=std::find_if(m.begin()、m.end()、FindByName(“bob”);
如果(it!=m.end())m.erase(it);

这毫无意义。为什么要按值搜索,按键操作?不应该恰恰相反吗?如果您需要两种类型的查找映射,请尝试boost.bimap!然后,只需说一句“擦除”。这就是重点——我希望在某个时候清理对
我的类
对象的引用,然后(使用描述)重新创建一个对象,并将新对象的ptr放入地图中,放入原始ptr所在的位置。。。。还是不明白。为什么使用共享指针作为映射键?如果你能更抽象地描述你的目标,也许我们能想出一个更好的设计?