草庐IT

together_unique

全部标签

c++ - 将 std::unique_ptr 与标准容器一起使用

当我意识到C++11添加了unique_ptr时,我一直在寻找一种方法来实现安全vector和动态指针映射。我研究了如何在Google上使用它们,但未能找到详细信息。我需要知道的是:除了自动内存收集之外,指针和unique_ptr之间到底有什么不同?如何从vector或map中删除unique_ptr?除了删除迭代器之外,我还必须使用任何特殊代码吗? 最佳答案 没有。unique_ptr只是一个指针的包装器,它会在unique_ptr被销毁时删除指针。它没有开销(就像它替换的auto_ptr模板一样)。不,它会起作用的。困难实际上来

c++ - 使用 unique_ptr 控制文件描述符

理论上,我应该能够使用自定义指针类型和删除器,以便让unique_ptr管理一个不是指针的对象。我尝试了以下代码:#ifndefUNIQUE_FD_H#defineUNIQUE_FD_H#include#includestructunique_fd_deleter{typedefintpointer;//Internaltypeisapointervoidoperator()(intfd){close(fd);}};typedefstd::unique_ptrunique_fd;#endif//UNIQUE_FD_H这不起作用(带有-std=c++11参数的gcc4.7)。它响应以下错

c++ - 尝试从 vector 中获取 unique_ptr 元素后出错

我阅读了有关如何插入unique_ptr的信息进入vector>:WhycanInotpush_backaunique_ptrintoavector?但是我如何取回一个元素呢?我举了个例子:#include#includeclassA{public:A(intx,inty){x_=x;y_=y;}private:intx_;inty_;};intmain(){std::vector>vec_a;std::unique_ptrtmp_a=std::unique_ptr(newA(13,32));vec_a.push_back(std::move(tmp_a));vec_a.push_ba

c++ - unique_lock 与使用互斥锁相比有什么特殊用途?

我不太清楚为什么std::unique_lock比仅使用普通锁有用。我正在查看的代码示例是:{//aquirelockstd::unique_locklock(queue_mutex);//addtasktasks.push_back(std::function(f));}//releaselock为什么这个比queue_mutex.lock();//addtask//...queue_mutex.unlock();这些代码片段是否完成了同样的事情? 最佳答案 [Do]thesesnippetsofcodeaccomplishthe

c++ - 工厂、unique_ptr 和 static_cast

考虑具有基对象、派生接口(interface)和最终对象的多态类://baseobjectstructobject{virtual~object()=default;};//interfacesderivedfrombaseobjectstructinterface1:object{virtualvoidprint_hello()const=0;templatestaticvoidon_destruction(object*/*ptr*/){std::cout在实际用例中,最终对象是通过插件系统定义的,但这不是重点。请注意,我希望能够在销毁对象时调用on_destruction(请参阅

c++ - Eclipse CDT 索引和 std::unique_ptr

我在这段代码中使用了std::unique_ptr,它按我预期的方式编译和运行。std::stringstreamout;outs(newstd::string(out.str()));s->insert(s->end()-2,1,'.');returnstd::move(s);但是,我从EclipseCDT收到错误消息。第四行:Method'insert'couldnotberesolved,Method'end'couldnotberesolved.以前,我也遇到过出现名称std::unique_ptr的错误。这已通过设置预处理器符号__GXX_EXPERIMENTAL_CXX0X

c++ - 升压::变体; std::unique_ptr 和复制

这个问题确定不可复制类型不能与BoostVariant一起使用树类templateclassTree{private:classTreeNode{public:std::unique_ptrNodesMoveconstructorsandmoveassignment+otherpublicmembersprivate:TreeNode(constTreeNode&other);(=deletenotsupportedoncompiler)TreeNode&operator=(constTreeNode&rhs);(=deletenotsupportedoncompiler)};//En

c++ - unique_ptr 的内存占用

这个问题在这里已经有了答案:Howcanstd::unique_ptrhavenosizeoverhead?(2个答案)关闭7年前。unique_ptr实例(没有自定义删除器)是否具有与原始指针相同的内存占用空间,或者实例存储的不仅仅是指针?

c++ - 如果键已经存在,则在不删除指针的情况下将 unique_ptr 插入映射的有效方法

这个问题在这里已经有了答案:C++insertingunique_ptrinmap(3个答案)关闭3年前。简单的方法显然是std::map>mymap;autof=mymap.find(5);std::unique_ptrmyptr;if(f==mymap.end())mymap.insert({5,std::move(myptr)});但是,这看起来效率不高,因为我必须在map中找到key两次。一个检查键是否不存在,插入函数也会做同样的事情。如果我简单地使用mymap.insert({5,std::move(myptr)});然后如果pair.second返回false(key已存在

c++ - 将 std::unique_ptr 类成员标记为 const

许多使用std::unique_ptr来管理类依赖项所有权的示例如下所示:classParent{public:Parent(Child&&child):_child(std::make_unique(std::move(child))){}private:std::unique_ptr_child;};我的问题是将_child成员标记为const是否会产生意想不到的副作用?(除了确保不能在_child上调用reset()、release()等)。我问是因为我还没有在示例中看到它,也不知道这是故意的还是只是为了简洁/一般性。 最佳答案