拥有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?
所以我有这样的函数:
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);
相关文章:
- CLANG 编译器 说:变量"PTR"可能未初始化
- 在以唯一ptr为值的C++映射中,动态内存何时会被销毁
- 将 ptr 传递给 ptr 到 A 作为参数传递给 A 的函数是不好的做法吗?
- 为共享 ptr 向量实现复制 c'tor?
- 字符和整数中 **(ptr+1) 的值差异
- C++:在不中断共享的情况下通过引用传递共享 PTR?
- 如何将派生类从基 ptr 分配给 nlohmann::json
- 引用 std::shared:ptr 以避免引用计数
- 为什么我不能在不进行任何转换的情况下将浮点数放入任何类型的 ptr 中?
- 在调用函数时,ptr** 和 ptr*& 之间是否有区别,或者首选C++?
- 在反转字符串'my.name.is'时,我得到的输出为"is@.name.my"。我不明白'@'是从哪里来的
- 另一种类型的智能ptr,比如具有弱refs的unique_ptr
- My Project的属性配置正确,但Visual Studio仍然无法打开包含头文件
- 尝试打印出 *ptr++ 的值,以了解它是如何工作的
- 如何控制共享 ptr 引用计数?
- C++中的指针否定 (!ptr == NULL)
- 从const ptr*转换为ptr*时出现问题
- 这是MSVC 2013中具有共享PTR的单例的正确实现吗?
- 对唯一 ptr 无效读取的引用向量
- C++ 类型转换基础 PTR 到派生 PTR 保存在引用类中