草庐IT

EidosValue_Int_vector

全部标签

c++ - 为什么采用 int 的函数重载优于采用 unsigned char 的函数重载?

考虑这个程序:#includeusingnamespacestd;voidf(unsignedcharc){cout这会打印出97,表明选择的f()重载是采用int的重载。我觉得这很奇怪;直觉上unsignedchar不是更适合char吗? 最佳答案 wouldn'tintuitivelyanunsignedcharbeabettermatchforachar?嗯,我想,但不是根据标准。根据[conv.prom]p1:Aprvalueofanintegertypeotherthanbool,char16_­t,char32_­t,o

c++ - shared_ptr 中对 const int 的 undefined reference

我有一个配置类//config.hppclassConfig{public:staticconstexprinta=1;staticconstexprintb=1;}并包含在main.cpp中//main.cpp#include"config.hpp"intmain(){std::coutstream=std::make_shared(Config::a);//compileerror}编译器说未定义对Config::a的引用它在使用cout时有效,但在shared_ptr构造函数中时无效。我不知道为什么会这样。 最佳答案 请注意s

c++ - int() 和 int(*)() 有什么区别?

我正在尝试创建一个识别函数的类模板,我可以在其中识别函数何时专门化R(*)()的类模型,但在std::function你可以申报return_type(),和std::is_same.::value为零。这是什么int()statementmean和int()有什么区别和int(*)()?更新:所以int()是函数声明或函数类型并且int(*)()是指向函数的指针。但是waht是int(std::string::)()的类型函数?类似于intstd::string::(),或喜欢std::functionint(conststd::string&)?如何让这个程序输出1?#includ

C++ vector 元组,按索引从元素创建元组

我有一个模板类,它有元组,由vector填充。templateclassMyClass{public:std::tuple...>vectors;};我想获取由指定索引上的vector元素填充的新元组。templateclassMyClass{public:std::tuple...>vectors;std::tupleelements(intindex){//HowcanIdothis?}};这可能吗? 最佳答案 您可以在C++14中使用接受indexsequence的辅助函数的常用技术相当轻松地完成它作为附加参数:templat

c++ - 不同类型的 `decltype((const int)a)` 和 `decltype((const int)1)`

//g++7.3templatestructtd;intmain(){inta=1;tdt1;tdt2;return0;}编译结果如下:错误:聚合‘tdt1’类型不完整,无法定义错误:聚合‘tdt2’类型不完整,无法定义那么,为什么decltype((constint)a)的类型是和decltype((constint)1)不一样? 最佳答案 说明符decltype((constint)a)和decltype((constint)1)都解析为int。这是因为没有const非类类型的纯右值,如C++17[expr]中所述:Ifaprv

c++ - 如何将所有指针从一个 vector 移动到另一个 vector ?

基本上我想做的是删除vector中的一些指针,但我发现在vector中间这样做可能会很慢。所以我有一个vector,里面已经有数据了:std::vectorvec1;//Thisalreadycontainspointers我将遍历vec1并将一些指针添加到另一个vector(vec2):vec2.push_back(vec1.at(index))现在我想做的是vec1=vec2但我不知道这是否是更好(有效)的方法。最好的方法是什么?我试过:虽然遍历vec1只是删除我需要从中删除的内容:it=vec1.erase(it)在遍历vec1时将最后一项移动到实际索引并poping_backv

c++ - std::vector 的容量会减少吗?

C++14finalworkingdraft对std::vector做出以下评论:Storagemanagementishandledautomatically,thoughhintscanbegiventoimproveefficiency.cppreference说:Thestorageofthevectorishandledautomatically,beingexpandedandcontractedasneeded.和WikipediaentryforDynamicarray说:C++'sstd::vectorandRust'sstd::vec::Vecareimplemen

c++ - struct S { int align; 之间的区别}; (在 struct 关键字之后命名)和 struct { int align; } S; (结构定义后的名称)

#includestructHeader{unsignedlonglongintalignment;};intmain(void){structHeaderheader;//note:wecanloosethe'struct'inC++structHeader*pheader=&header;return0;}上面的程序在C和C++中都能完美编译。但是当我将Header结构更改为:struct{unsignedlonglongintalignment;}Header;它失败并在C中显示以下消息:错误:“Header”的存储大小未知在C++中:error:aggregate‘main()

c++ - 为什么 const vector<const pair<...>> 给出 'cannot be overloaded' 错误?

我有这个简单的代码:#include#includevoidfoo(conststd::vector>&networks){for(autop:networks){}}voidbla(conststd::vector>&networks){for(autop:networks){}}这会在bla()中产生一个错误:mrvn@frosties:~%g++-O2-W-Wall-g-std=gnu++17-cbla.ccInfileincludedfrom/usr/include/x86_64-linux-gnu/c++/5/bits/c++allocator.h:33:0,from/usr

c++ - 从两个 vector 中删除公共(public)实体?

假设我有vector,vector如何从它们中删除公共(public)实体我已经为class1对象class1a,class1b定义了==operator 最佳答案 stlalgorithms提供几个函数来执行集合操作,特别是计算setsymmetricdifference,这就是您所需要的。这是一个使用示例:#include#includeintmain(intargc,char**argv){std::vectorv1;v1.push_back(1);v1.push_back(2);v1.push_back(3);v1.push