草庐IT

C++ set 和 shared_ptr

我有X类:classX{public:booloperator然后我有以下代码:std::multiset>m;我的问题是:m中的数据是如何排序的?X(shared_ptr)的地址或X.operator?如果按地址X订购,我怎样才能按X.operator订购呢??为此m,如果我想从小到大访问它的元素,下面的代码能保证吗?如果没有,怎么办?for(auto&i:m){f(i);} 最佳答案 您的集合是根据您的key_type排序的,即std::shared_ptr.作为您的std::shared_ptr是comparable,std:

c++ - 为 std::shared_ptr 指定一个删除器,它适用于特定类型或其派生类型的所有对象

我的项目中有以下类classBase{public:virtual~Base(){};}classDer1:publicBase{public:virtual~Der1(){};}classDer2:publicBase{public:virtual~Der2(){};}我将这些类的对象保存为std::shared_ptr。我需要为类型为Base或其任何derivedtypes的所有对象提供自定义删除器。我想要的删除方法中的代码将对所有这些对象做同样的事情,比如classDeleter{public:voidoperator()(Base*b){//Dosomethingdeleteb

c++ - std::shared_ptr:带有自定义删除器的 typedef

这个问题在这里已经有了答案:Typedefashared_ptrtypewithastaticcustomdeleter,similartounique_ptr(2个答案)关闭7年前。我正在使用SDL2开发C++应用程序,并希望使用shared_ptr来保存指针内容。所以我做例如这个:typedefstd::shared_ptrSDLWindowPtr;而且我需要在整个过程中使用自定义删除器。是否可以将其包含在typedef中?如果是,如何?删除函数称为SDL_DestroyWindow。如果没有,如何让shared_ptr使用SDL_DestroyWindow作为自定义删除函数?提前

c++ - 为什么存在 shared_ptr 的原子重载

为什么有shared_ptr的原子重载,如here所述而不是为处理shared_ptr的std::atomic专门化。似乎与其余C++标准库采用的面向对象模式不一致。在使用shared_ptr实现readcopyupdateidiom时,为了确保我做对了我们需要通过这些函数对共享指针进行所有访问(读取和写入)吗? 最佳答案 因为:std::atomicmaybeinstantiatedwithanyTriviallyCopyabletypeT.来源:http://en.cppreference.com/w/cpp/atomic/at

c++ - 为什么智能指针 vector 不是指向实现与该接口(interface)协变的接口(interface)的项目?

为什么智能指针vector不与item实现的接口(interface)协变?例如如果我有一个指向狗的指针vector,为什么我不能将它用作指向iAnimal的指针vector?#include#include#include#includestructiAnimal{virtualstd::stringspeak()const=0;};structiMammal:publiciAnimal{virtualstd::stringspeak()const=0;virtualintlegs()const=0;};structDog:publiciMammal{std::stringspeak

c++ - 在包含 shared_ptr 的 map 上使用 find_if 会增加引用计数

我正在创建一个程序,它有一个包含shared_ptr的映射。当我尝试使用std::find_if在其中查找元素时,shared_ptr的引用计数会增加。示例:#include#include#include#includeintmain(void){std::map>map;map[1]=std::make_shared(3);map[2]=std::make_shared(5);map[4]=std::make_shared(-2);autoit=std::find_if(map.begin(),map.end(),[](conststd::pair>&elem){std::cout

c++ - 在 GCC 中使用 shared_ptr 的可移植方式

GCC4.1使用header和GCC4.3使用header,我需要一种可移植的方式来使用shared_ptr使用GCC4.3.2和GCC4.2.1,有没有办法在不检查GCC版本宏或使用外部库(如Boost)的情况下做到这一点? 最佳答案 仍将与gcc4.3一起工作。如果您想同时支持这两个版本,只需使用tr1姓名。 关于c++-在GCC中使用shared_ptr的可移植方式,我们在StackOverflow上找到一个类似的问题: https://stackove

c++ - 为什么 shared_ptr<void> 而不是 shared_ptr<HANDLE>

基于http://en.highscore.de/cpp/boost/smartpointers.html#smartpointers_shared_pointer#include#includeintmain(){boost::shared_ptrh(OpenProcess(PROCESS_SET_INFORMATION,FALSE,GetCurrentProcessId()),CloseHandle);SetPriorityClass(h.get(),HIGH_PRIORITY_CLASS);}问题:为什么h定义为boost::shared_ptr而不是boost::shared_

c++ - std::find 跨一组 shared_ptr

我确定我在这里做了一些愚蠢的事情,但我看不到它。为什么不能编译以下内容?#include#include#include#include//Aclasstoplaywith.Encapsulatesaname.classStringClass{public:StringClass(std::stringconst&name):MyName(name){}std::stringconst&Name()const{returnMyName;}private:std::stringMyName;};//Thesetofinstancesof"StringClass".std::vector>

c++ - std::static_pointer_cast 是否有任何额外的运行时开销?

相对于static_cast,即。所以,如果我们有这两个类型转换Base*b(newDerived());Derived*d=static_cast(b);//(1)shared_ptrb(newDerived());shared_ptrd=static_pointer_cast(b);//(2)第(2)行会比第(1)行慢吗? 最佳答案 是的,它有更多的开销,因为它必须返回一个新的shared_ptr而不是一个新的原始指针。boost实现是:templateshared_ptrstatic_pointer_cast(shared_p