从weak_ptrs列表中删除项目
removing item from list of weak_ptrs
我有一个用于跟踪对象的weak_ptrs列表。在某个时候,我想从给定shared_ptr或weak_ptr的列表中删除一个项目。
#include <list>
int main()
{
typedef std::list< std::weak_ptr<int> > intList;
std::shared_ptr<int> sp(new int(5));
std::weak_ptr<int> wp(sp);
intList myList;
myList.push_back(sp);
//myList.remove(sp);
//myList.remove(wp);
}
但是,当我取消注释上述行时,程序不会构建:
1>c:program files (x86)microsoft visual studio 10.0vcincludelist(1194): error C2678: binary '==' : no operator found which takes a left-hand operand of type 'std::tr1::weak_ptr<_Ty>' (or there is no acceptable conversion)
如何在给定shared_ptr或weak_ptr的情况下从列表中删除项目?
弱指针没有运算符==。您可以比较weak_ptrs指向的shared_ptrs。
比如这样。
myList.remove_if([wp](std::weak_ptr<int> p){
std::shared_ptr<int> swp = wp.lock();
std::shared_ptr<int> sp = p.lock();
if(swp && sp)
return swp == sp;
return false;
});
相关文章:
- 如何维护资源管理器项目视图中当前可见的项目列表
- 你能检查一下为什么在这个代码中从链接列表中删除项目不起作用吗
- C++ 从链表中删除项目时出现问题
- 在给定的项目列表上实现搜索?
- 从 std::vector 中删除项目时"Iterator not incrementable"
- 从 QTreeView 中删除项目时取消选择所有行
- 从传递给新线程C++的矢量中删除项目
- 从文件中删除单词列表的最快方法
- 在修改项目列表时,请防止可编辑的Qcombobox选择更改
- 如何创建一个函数,该函数可以从一个qtreewidget中删除项目并将其添加到另一个函数
- 在拖放 QML 列表中插入/删除项目使用 cpp 模型查看
- 如何在不删除C++集中删除项目?
- 删除对象列表中的类似项目
- 从C 列表中删除项目
- 如何从std::列表中删除项目
- 通过函数后从列表中删除项目时出错
- 从weak_ptrs列表中删除项目
- 在列表框删除项目时收到通知
- 从 std::vector<std::vector 中删除任意项目列表<T> >
- 我无法让我的删除功能从列表中删除项目