草庐IT

c++ - 在为它分配新值之前应该将 `nullptr` 分配给 `std::unique_ptr` 吗?

#include#includeusingnamespacestd;intmain(){std::unique_ptrptrA=std::make_unique(10);ptrA=std::make_unique(20);//caseIreturn0;}#include#includeusingnamespacestd;intmain(){std::unique_ptrptrA=std::make_unique(10);ptrA=nullptr;//caseIIorptrA.reset()ptrA=std::make_unique(20);return0;}我见过很多人使用CaseII

c++ - 为什么 std::unique_ptr 没有 const get 方法?

我知道std::unique_ptr是这样的,可能不会改变以破坏向后兼容性,但我想知道是否有人有充分的理由说明规范的作者没有这样做'用看起来像这样的const变体重载get方法constT*get()const;遵循unique_ptr为const的意图。我最好的猜测是它试图镜像指针并像T*const而不是典型的类。作为后续问题,如果我想在类的const实例中以类似const的方式保存指针,我是否应该使用std::unique_ptr以外的其他东西来保存数据?更新在我的例子中,我想保护自己不在类本身中滥用指针。我正在编写一个const移动构造函数MyClass(constMyClass

c++ - std::shared_ptr 的用法

如何将std::shared_ptr用于double组?此外,使用shared_ptr的优点/缺点是什么。 最佳答案 这取决于你追求的是什么。如果您只想要一个可调整大小的double数组,请使用std::vector例子:std::vectorv;v.push_back(23.0);std::cout如果共享所述数组的所有权对您很重要,请使用例如std::shared_ptr>例子:std::shared_ptr>v1(newstd::vector);v1->push_back(23.0);std::shared_ptr>v2=v1

c++ - 在 direct3d11 对象上使用 std::shared_ptr 的自定义删除器

当我使用std::shared_ptr并需要一个自定义删除器时,我通常会创建一个对象的成员函数来促进它的销毁,如下所示:classExample{public:Destroy();};然后当我使用共享ptr时,我只是这样:std::shared_ptrptr(newExample,std::mem_fun(&Example::Destroy));问题是,现在我正在使用d3d11,我想将com发布函数用作std::shared_ptr自定义删除器,就像这样std::shared_ptrptr(nullptr,std::mem_fun(&ID3D11Device::Release));但是

c++ - 如果我返回一个 unique_ptr,原始对象会被删除吗?

我想做这样的事情:unique_ptrMyFunc(){MyObj*ptr=newMyObj();...returnunique_ptr(ptr);}unique_ptrvalue=MyFunc();但我不确定在函数返回后临时值被销毁时对象是否会被删除。如果是这样,我应该如何正确实现返回unique_ptr的函数? 最佳答案 不,当函数作用域结束时,对象不会被删除。这是因为unique_ptr的move构造函数会将所有权语义“移动”到新的unique_ptr对象,并销毁旧的unique_ptr不会导致删除分配的对象。注意:这不是正确

c++ - 标准库中rbegin和end函数的区别

我有一个映射的实现,其中ID存储为值,标记为键。这使我能够利用map中的自动排序功能,并让我识别得分最高的元素的ID。for(map::iteratori=marks.begin();i!=marks.end();++i)coutfirstsecondsecondsecond产生这个输出:31234204512275211420输入序列是值的递增顺序。为什么end()不显示“1”而是显示最后输入的一对key?rbegin()和end()有什么区别? 最佳答案 rbegin实际上是容器的最后一个元素。end是容器末尾的过去。所以mar

c++ - Unique_ptr 的 STL vector - 如何重置?

我创建了两个标准的unique_ptrvector:std::vector>students;std::vector>teachers;然后,我创建一个新对象并将其放入vector中:students.push_back(std::unique_ptr(newStudent()));teachers.push_back(std::unique_ptr(newTeacher()));完成所有操作后,如何删除vector?Whitoutunique_ptr我不得不做一个循环并删除每个对象:while(!students.empty()){deletestudents.back();stud

c++ - QScopedPointer、boost::scoped_ptr - 为什么提示类型不完整?

我有一个c-Structure,我想将它嵌入到一个cpp类中而不破坏我的全局命名空间,所以我不想包含c-header。这就是为什么我想使用具有前向声明结构名称的智能作用域指针(QScopedPointer或boost::scoped_ptr)。我不明白的是上述两个在编译时失败的作用域指针的实现:boost:errorC2027:useofundefinedtype'xxx'templateinlinevoidchecked_delete(T*x){//intentionallycomplex-simplificationcausesregressionstypedefchartype_

C++ shared_ptr 释放所有权

这个问题在这里已经有了答案:Moveownershipfromstd::shared_ptrtostd::unique_ptr(1个回答)关闭7年前。我们都知道在C++中我们可以很容易地将unique_ptr转换为shared_ptr。但是,如果我进行了这样的转换怎么办:-unique_ptru=make_unique();//Xissomeclassshared_ptrs=move(u);//thisworksofcourse现在我想将s中指针的所有权转移回u。遗憾的是,shared_ptr中没有像unique_ptr中那样的release()函数,否则我可能不会这样做:-u.res

c++ - 使用 std::begin 和 std::end 进行 vector 初始化

为什么可行?有两个不同的字符串"testString"但vector大小分配正确。#include#include#includeintmain(){std::vectorstr;str.assign(std::begin("testString"),std::end("testString"));copy(str.begin(),str.end(),std::ostream_iterator(std::cout,""));std::cout 最佳答案 你很幸运,编译器执行了stringpooling优化。请注意,您所做的仍然是未定