草庐IT

auto-size

全部标签

c++ - 对于具有默认分配器的标准容器,std::container::size_type 是否保证为 size_t?

喜欢:std::string::size_typestd::list::size_typestd::map::size_typestd::vector::size_type等等两者都是cplusplus.com和cppreference.com说他们通常是size_t,但它们是否真正、明确地保证为size_t的标准除非使用自定义分配器? 最佳答案 对于STL容器-不。[container.requirements.general]中标准的表96,其中列出了任何容器的容器要求X,解释得很清楚:但是,对于basic_string,siz

c++ - std::vector::resize(size_type) 需要 CopyInsertable?

这个问题是在我回答thisanotherquestion的时候提出的.N333723.3.6.3“vector容量”说(在770页):voidresize(size_typesz);Effects:Ifsz,equivalenttoerase(begin()+sz,end());.Ifsize(),appendssz-size()value-initializedelementstothesequence.Requires:TshallbeCopyInsertableinto*this.然而,clang++saysit'sokaythoughTisnotcopyable.我认为resiz

c++ - auto stdMaxInt = std::max<int> 的类型推导失败;

使用GCC4.8.4和g++--std=c++11main.cpp输出以下errorerror:unabletodeduce‘auto’from‘max’autostdMaxInt=std::max;对于这段代码#includetemplateconstT&myMax(constT&a,constT&b){return(a;myMaxInt(1,2);autostdMaxInt=std::max;stdMaxInt(1,2);}为什么它适用于myMax但不适用于std::max?我们可以让它与std::max一起工作吗? 最佳答案

c++ - constexpr if with initializer 由标准保证吗? 'constexpr(constexpr auto x = f(); x) { }'

我找不到任何关于新C++17if初始化语法的信息和“constexprif”在:http://open-std.org/JTC1/SC22/WG21/docs/papers/2016/p0128r1.html不过,Clang-HEAD支持该语法...constexprautof(){returntrue;}intmain(){ifconstexpr(constexprautox=f();x){}}在线代码在这里->http://melpon.org/wandbox/permlink/dj3a9ChvjhlNc8nr是constexprif带有标准保证的初始值设定项,如constexpr

c++ - 错误 : ‘template<class> class std::auto_ptr’ is deprecated

我正在使用scons和ubuntu。当我使用'scons'制作一些程序时,会发生错误,例如,src/db/DBTextLoader.cc:296:3:error:‘templateclassstd::auto_ptr’isdeprecated[-Werror=deprecated-declarations]/usr/include/c++/5/bits/unique_ptr.h:49:28:note:declaredheretemplateclassauto_ptr;这是我的命令;$./configuer$sourcesomething.sh$scons其实我也不知道。我已经在搜索这个

c++ - 制作 std::vector capacity>=N 和 size=0 的最佳方法?

给定一个std::vector,其大小和容量可以是任意的,将其大小更改为0并将容量更改为至少N(给定数字)的最佳做法是什么?我的直接想法是:voidf(vector&t,intN){t.clear();t.reserve(N);}但是我注意到了Areallocationisnotguaranteedtohappen,andthevectorcapacityisnotguaranteedtochange(whenstd::vector::cleariscalled).所以我想知道当原始容量大于给定的N时如何避免重新分配? 最佳答案 w

c++ - 为什么 GCC 中 std::list O(n) 的 size() 方法?

在GCC中,std::list的size()方法是O(n)。为什么?对于C++11,标准是size()oflistshouldbeO(1)但是在glibc中我们有以下内容:/usr/include/c++/4.6.3/bits/stl_list.htemplate>classlist:protected_List_base{...size_typesize()const{returnstd::distance(begin(),end());}问题是:为什么三年前的要求还没有在GCC中实现?编辑:gcc5改变了这一点:尽管以ABI改变为代价;这意味着使用gcc5.0编译的C++代码将无法

具有非 size_t 整数的 std::array 的 C++ 模板参数推导

我正在尝试调整Avoidingstructinvariadictemplatefunction中提供的解决方案根据我的需要。但是,我无法理解G++的行为。考虑以下功能:templateintnextline(consttypenamestd::arrayar){return0;}然后调用nextline(std::array{1,0});与GCC提示不匹配eslong.cpp:Infunction‘intmain()’:eslong.cpp:10:38:error:nomatchingfunctionforcallto‘nextline(std::array)’nextline(std

c++ - 为什么随机访问迭代器的算术运算符接受/返回 int 而不是 size_t?

由于std::vector上的大多数操作都需要/返回size_t-这就是我用于索引的类型。但现在我已经启用所有编译器警告来修复一些我知道的有符号/无符号转换问题,这条消息让我感到惊讶:warningC4365:'argument':conversionfrom'size_t'to'__w64int',signed/unsignedmismatch它是由这段代码生成的:std::vectorv;size_tidx=0;v.insert(v.begin()+idx+1,0);我收到很多其他类似的消息,建议迭代器的算术运算符接受并返回int。为什么不是size_t?修复所有这些消息很痛苦,并

c++ - 如何在 C++11 中将容器 std::array<type, size> 用于多维数组?

例如,包含三个整数的一维数组可以定义为std::arraymyarray或myarray[3].有没有像std::array这样的容器对于像myarray[3][3]这样的多维数组? 最佳答案 一个关键部分是确保{}初始化工作类似于std::array,并尽可能合理地让自己保持pod状。与std::array的兼容性也很重要,什么比std::array更兼容??所以我的解决方案从std::array中生成多维数组小号:templatestructmulti_array_helper{usingtype=T;};templateusi