草庐IT

together_unique

全部标签

c++ - 从带有原始指针的 vector 中删除 std::unique_ptr 的最佳方法?

所以我有一个像这样的vector:std::vector>myVector;然后我有另一个vector,其中包含SomeClass的原始指针:std::vectormyOtherVector;如果myOtherVector中有一个元素,它也会在myVector中,所以我想遍历myOtherVector中的每个元素并删除来自myVector的相同元素。然后清除vector。这是我想出的:for(size_ti=0;i这会产生编译时错误,因为myVector拥有唯一的指针,但我给remove()函数一个原始指针。这是我需要帮助的地方,因为我不知道解决这个问题的正确方法是什么。我将行更改为:

c++ - 从 unique_ptr<char[]> 到 unique_ptr<const char[]>

移动unique_ptr的最佳成语是什么?到unique_ptr?用例:假设您在某个缓冲区中创建了一个C字符串。为确保在出现异常时进行正确清理,可以使用unique_ptr引用该缓冲区.构造字符串后,您可能希望将其移动到某个类成员,声明为unique_ptr。以避免进一步修改字符串。这是我迄今为止最好的:std::unique_ptrres;std::unique_ptrbuf(newchar[4]);buf[0]='f';buf[1]=buf[2]='o';buf[3]='\0';res=std::unique_ptr(const_cast(buf.release()));单纯的移动

c++ - 为什么 shared_ptr 签名与数组的 unique_ptr 不同?

std::unique_ptrp(newint[10]);//okstd::shared_ptrp(newint[10]);//Errorshared_ptrsp(newint[10],[](int*p){delete[]p;});//Ok,writingcustomdeleterfor//arraysinceshared_ptrwillcall//deletebydefault.与unique_ptr相比,数组的shared_ptr签名有什么不同的具体原因吗?如果两个api都遵循类似的签名,那就更简单了。 最佳答案 unique_

c++ - 在 VS2013 上测量 vector<unique_ptr> 的性能?

TL;DR是VS2013的优化器混淆了,还是我的测量有误,或者全局虚拟变量实际上是否需要可变才能使测试有效或____?免责声明:这主要是出于“学术”兴趣,我不认为我看到的差异会真正影响任何生产代码。简介:我最近的一些测量让我找到了thisquestion因为我发现std::vector>之间存在显着差异和boost::ptr_vector在VS2013上。(另见评论there)看来,对于我的特定测试用例,访问boost::ptr_vector中的元素比使用unique_ptrvector快50%!我的测试代码在这里:http://coliru.stacked-crooked.com/a

c++ - 如何正确地将所有权从原始指针移动到 std::unique_ptr?

我的做法是:classSomeClass{std::vector>myObjects;public:voidtakeOwnership(MyObject*nowItsReallyMyObject){myObjects.emplace_back(std::move(nowItsReallyMyObject));}};我做的每件事都正确吗?有没有更好的解决方案? 最佳答案 move是多余的。我自己,我会这样做:voidtakeOwnership(std::unique_ptrnowItsReallyMyObject){myObjects

c++ - 如何使用 lambda 作为 std::unique_ptr 的删除器?

检查以下设计的程序:#include#includetemplateusingUniPtr=std::unique_ptr>;int*alloc(){returnnewint;}UniPtrfunc(){autodealloc=[](int*p){deletep;};returnUniPtr{alloc(),dealloc};}intmain(){autop=func();return0;}来自std::functionconstructormanual,我认为构建std::function对象可能会抛出异常,即使这个比例很低:UniPtrfunc(){autodealloc=[](i

c++ - 为什么我的 unique_ptr 认为它有一个空函数指针删除器?

这个问题在这里已经有了答案:Cannotmovestd::unique_ptrwithNULLdeletertostd::shared_ptr?(2个答案)关闭3年前。我正在尝试使用C++学习SDL。我创建了一个window.hheader和一个window.cpp源文件来存储Window类。在window.h中,它看起来像这样:ClassWindow{public:Window();...private:std::unique_ptrwindow;std::unique_ptrrenderer;...}省略了类中的一些代码。然后,在我的源文件中,在默认构造函数的定义中,我这样做:Wi

C++ 零规则 : polymorphic deletion and unique_ptr behavior

在最近overloadjournal在执行零规则主题下,作者描述了我们如何避免编写五个运算符的规则,因为编写它们的原因是:资源管理多态性缺失这两个都可以通过使用智能指针来解决。这里我特别感兴趣的是第二部分。考虑以下代码片段:classBase{public:virtualvoidFun()=0;};classDerived:publicBase{public:~Derived(){coutpB=make_shared();pB->Fun();}在这种情况下,正如文章作者所解释的那样,我们通过使用共享指针进行多态删除,这确实有效。但是如果我将shared_ptr替换为unique_ptr

c++ - 使用 operator== 时 std::set 中 unique_ptr 的深度比较

我正在尝试使用std::set将一组unique_ptr保存到我定义的自定义对象中。我在定义集合时提供了一个自定义比较函数(以启用深度比较)。在将元素插入集合时,此比较功能似乎可以正常工作,即具有相同内容的项目不会被插入两次。但是,如果我使用operator==比较两个集合,它似乎会被忽略,即具有等效元素的集合返回为不相等,而我期望(希望)它相等(因为我提供的自定义比较功能会进行深度比较)。compare函数是否只在插入时使用?如果是这样,是否有替代方法让operator==进行深度比较?感谢任何指点。谢谢:)示例代码////main.cpp//Test#include#include

c++ - 如何用元组转发unique_ptr?

想象一个函数期望对std::unique_ptr的右值引用。voidfoo(std::unique_ptr&&a);在我的真实示例中,有不止一个参数,因此我决定使用std::tuple将参数转发给它右值引用-所以std::forward_as_tuple:voidforward_to_foo(std::tuple&&>&&t);intmain(){forward_to_foo(std::forward_as_tuple(std::make_unique(8)));}目前一切正常当我想“解压”这个元组时出现问题voidforward_to_foo(std::tuple&&>&&t){fo