Using a std::unordered_set of std::unique_ptr
来源:互联网 发布:软件开发企业收入确认 编辑:程序博客网 时间:2024/05/20 20:21
转自:https://stackoverflow.com/questions/17851088/using-a-stdunordered-set-of-stdunique-ptr
====================================Ask====================================
Assume I have a set of unique_ptr:
std::unordered_set <std::unique_ptr <MyClass>> my_set;
I'm not sure what's the safe way to check if a given pointer exists in the set. The normal way to do it may be to call my_set.find ()
, but what do I pass as a parameter?
All I have from the outside is a raw pointer. So I have to create another unique_ptr from the pointer, pass it to find()
and then release()
that pointer, otherwise the object would get destructed (twice). Of course, this process can be done in a function, so the caller can pass the raw pointer and I do the conversions.
Is this method safe? Is there a better way to work with a set of unique_ptr?
===================================Answer==================================
You can also use a deleter that optionally doesn't do anything.
template<class T>struct maybe_deleter{ bool _delete; explicit maybe_deleter(bool doit = true) : _delete(doit){} void operator()(T* p) const{ if(_delete) delete p; }};template<class T>using set_unique_ptr = std::unique_ptr<T, maybe_deleter<T>>;template<class T>set_unique_ptr<T> make_find_ptr(T* raw){ return set_unique_ptr<T>(raw, maybe_deleter<T>(false));}// ...int* raw = new int(42);std::unordered_set<set_unique_ptr<int>> myset;myset.insert(set_unique_ptr<int>(raw));auto it = myset.find(make_find_ptr(raw));
- Using a std::unordered_set of std::unique_ptr
- std::unique_ptr
- 实战c++中的智能指针unique_ptr系列-- 使用std::unique_ptr代替new operator(错误:‘unique_ptr’ is not a member of ‘std’)
- 关于std:auto_ptr std:shared_ptr std:unique_ptr
- Using C++ std::equal on a container of shared_ptr
- std::unique_ptr和std::shared_ptr的用法
- C++11 智能指针std::shared_ptr/std::unique_ptr/std::weak_ptr
- vc++ .net std::string is not a member of std
- 'find_if’ is not a member of ‘std'
- 'numeric_limits' is not a member of 'std'|
- 'numeric_limits' is not a member of 'std'
- c++11 container liber: std::queue std::map std::vector std::unique_ptr
- 智能指针:从std::auto_ptr到std::unique_ptr
- C++11引入智能指针std::unique_ptr
- C++11智能指针之std::unique_ptr
- using namespace std
- using namespace std
- using namespace std
- 关于getRawX和getX、getMeasureWidth和getWidth之间的区别
- 京东2018秋招编程题
- 《UNIX网络编程 卷2》 笔记: 记录上锁
- requests禁止重定向
- UVa 11307
- Using a std::unordered_set of std::unique_ptr
- 泛型栈
- 后端笔记1
- 慕课JAVA之文件传输基础----JAVA IO流
- S3c2440之时钟频率(修改综合版)
- ue4 行为树
- C++泛型
- Java9 模块化 Jigsaw 入门指引
- 使用公式C=(5/9)(F-32)打印下列华氏温度与摄氏温度对照表。