草庐IT

hash-code-uniqueness

全部标签

c++ - 如何对数组使用 unique_ptr

我有课classA{public:A(){couta(newA[5]);//-doesn'tworkunique_ptra(newA[1]);//-doesn'tworkunique_ptra(newA);//-works}为什么会这样?我猜是关于移动构造函数的(由于析构函数,它不能自动创建),但是为什么我们在这里需要一个移动构造函数?和之间有什么区别:unique_ptra(newA[1]);//-doesn'tworkunique_ptra(newA);//-works 最佳答案 要将unique_ptr与数组分配一起使用,您需

C++ 模板 : How to conditionally compile different code based on data type?

这里有一个小例子来说明我的问题的本质:#includeusingnamespacestd;typedefcharachar_t;templateclassSTRING{public:T*memory;intsize;intcapacity;public:STRING(){size=0;capacity=128;memory=(T*)malloc(capacity*sizeof(T));}constSTRING&operator=(T*buf){if(typeid(T)==typeid(char))strcpy(memory,buf);elsewcscpy(memory,buf);ret

c++ - 我可以将函数的输出参数存储到 unique_ptr 中吗?

我有以下代码:classThing{};voidfnc(Thing**out){*out=newThing();};其中fnc通过输出参数返回Thing的新实例。通常我会按如下方式使用它:intmain(){Thing*thing;fnc(&thing);}我可以将返回的对象放在std::unique_ptr中吗?intmain(){std::unique_ptruniqueThing;fnc(???);} 最佳答案 要扩展您的代码示例,它(即传递指针)将是voidfnc(std::unique_ptr*out){out->rese

c++ - 如何用空指针初始化 unique_ptr 的 vector ?

我需要初始化一个vector>与nullptr秒。this中的方法帖子太复杂了。我的情况比较特殊,只需要初始化为nullptr.我怎样才能实现它?我知道我可以使用for循环来push_back一个nullptr每一次。有什么优雅的方法吗?顺便说一句,make_unqiue不适用于我的编译器。#include#include#includeusingnamespacestd;structTNode{//charch;boolisWord;vector>children;TNode():isWord(false),children(26,nullptr){}};intmain(){TNod

JSON parse error: Illegal character ((CTRL-CHAR, code 31)): only regular white space (\r, \n, \t) is

JSONparseerror:Illegalcharacter((CTRL-CHAR,code31)):onlyregularwhitespace(\r,\n,\t)isallowedbetweentokens;nestedexceptioniscom.fasterxml.jackson.core.JsonParseException:Illegalcharacter((CTRL-CHAR,code31)):onlyregularwhitespace(\r,\n,\t)isallowedbetweentokensat[Source:(org.springframework.util.Strea

c++ - 单元测试、模拟和 unique_ptr

有一个正在测试的类目前接受unique_ptr&&在它的构造函数中,表示它想要获得接口(interface)实现的单一所有权。想要使用模拟Interface测试此类时会出现问题虽然:模拟框架(HippoMocks)只给我Interface*我不拥有,因此无法删除。我以前在测试constshared_ptr&的类(class)时遇到过同样的问题作为参数,但通过提供自定义的无操作删除器来修复:templatevoidNoDelete(T*){}//createashared_ptrwithouteffectivedeletertemplatestd::shared_ptrmock_shar

以非专用模板作为值的 C++ hash_map

我想要一个std::hash_map将(例如)常规std:string映射到多个不同的特化另一个模板类。这个例子是我想要实现的(虽然它是错误的并且无法编译):templateclassFoo{public:Foo(T_value){this->value=_value;}private:Tvalue;};intmain(){hash_mapvarious_foos;various_foos["foo"]=Foo(17);various_foos["bar"]=Foo(17.4);} 最佳答案 map只能存储单一的值类型,所以不能直接

c++ - 如何关闭 Code::Blocks 的自动完成

当我在Code::Blocks中输入代码时,我更愿意直接输入代码,而无需让编辑器插入右括号和方括号。我在“设置”>“编辑器”>“代码完成”中选择了禁用代码完成,但此功能不会禁用带有结束字符的自动完成功能。在Code::Blocks中关闭此功能的正确设置是什么?(请注意代词审查员可能会发生这种情况:https://www.theregister.co.uk/2019/10/08/stack_overflow_apology/) 最佳答案 “设置->编辑器...->常规设置->缩进选项->大括号补全”

c++ - std::unique_ptr 和指向指针的指针

我想结合使用std::unique_ptr和FreeImage的FITAG。普通C中的代码将是:...loadimage;FITAG*tag=NULL;FreeImage_GetMetadata(FIMD_EXIF_EXIF,bitmap,"Property",&tag);...dosomestuffwithtag;FreeImage_DeleteTag(tag);...deleteimage;我对unique_ptr的尝试:std::unique_ptrtag(NULL,&FreeImage_DeleteTag);FreeImage_GetMetadata(FIMD_EXIF_EXI

c++ - 当shared_ptr,当unique_ptr

这个问题在这里已经有了答案:Differencesbetweenunique_ptrandshared_ptr[duplicate](4个答案)关闭7年前。什么时候应该使用shared_ptr什么时候使用unique_ptr?例如在这个类中而不是node*应该是shared_ptr或unique_ptr。它取决于什么?classnode{private:node*parent;vectorchildren;/**txny*x-numerdrzewa*y-numerwezla*/stringid;typeNodetype;//0-term,1-funcpublic:node(node*p