草庐IT

c++ - std::map::size_type 对于 std::map 其 value_type 是它自己的 size_type

我有一个std::map,float>这占用了太多内存,为了使用更少的内存,我决定将唯一字符串映射到整数(例如std::map,其中每个新的唯一字符串都映射到map的当前size()),并将这些整数值用作映射的成对键(例如,std::map,float>)。而不是int,我想用std::map::size_type:usingmap_index=std::map::size_type;std::pairkey;当然,这不会编译,因为我需要为map提供参数列表:vector.cc:14:19:error:invaliduseoftemplate-name`std::map'without

C++ std::list 排序保留顺序

这个问题在这里已经有了答案:关闭11年前。PossibleDuplicate:Isstd::list::sortstable?C++std::list排序函数是否保证保留列表中相等元素的顺序?例如。如果我们在列表中有对象A、B和C,并且比较运算符被重载,因此A==C和B

c++ - 新分配的 std::vector<int> 元素是否初始化为 0?

假设我们使用std::vector或std::vector.随着vector大小的增长,新分配的元素会被默认初始化为0,还是程序员需要显式地将它们初始化为0? 最佳答案 新元素是值初始化的:[C++11:23.3.6.3/9]:voidresize(size_typesz);Effects:Ifsz,equivalenttoerase(begin()+sz,end());.Ifsize(),appendssz-size()value-initializedelementstothesequence.对于int和long这意味着0:[

c++ - std::sort 在 std:vector of pointers 上失败

以下代码在对vector进行排序时崩溃。#include#include#includeusingnamespacestd;structFoo{intx;//inty;Foo():x(0){}};structCmp{booloperator()(Foo*p1,Foo*p2)const{if(p1->x!=p2->x)returnp1->xx;//if(p1->y!=p2->y)returnp1->yy;returntrue;}};intmain(){vectorv;for(inti=0;i为什么会这样? 最佳答案 boolopera

c++ - 为什么我从 std::fmod 和 std::remainder 得到不同的结果

在下面的示例应用程序中,我使用std::fmod将953除以0.1计算浮点余数我所期望的是,由于953.0/0.1==9530,std::fmod(953,0.1)==0我得到0.1-为什么会这样?请注意,使用std::remainder我得到了正确的结果。即:std::fmod(953,0.1)==0.1//unexpectedstd::remainder(953,0.1)==0//expected两种功能的区别:根据cppreference.comstd::fmod计算以下内容:恰好是x-n*y的值,其中n是截断小数部分的x/ystd::remainder计算以下内容:精确值x-n

c++ - 如何处理 std::find_if() 返回 false?

以下示例取自thecplusplus.comreferencepage并更改为返回false://find_ifexample#include//std::cout#include//std::find_if#include//std::vectorboolIsOdd(inti){return((i%2)==1);}intmain(){std::vectormyvector;myvector.push_back(10);myvector.push_back(20);myvector.push_back(40);myvector.push_back(50);std::vector::it

C++ 参数的值在 std::vector 中的堆栈帧之间发生变化

我遇到了一个非常奇怪的错误,我希望有人能解释一下。我有一个简单的std::vector,其中V3x是一个3dvector(线性代数类型)。以下代码导致std::length_error抛出异常:std::vectorvertices;intvertexCount=computeVertexCount();vertices.resize(vertexCount);//throwsstd::length_error我已经验证computeVertexCount()返回值35,远低于vector::max_size()所以它不可能要求太多内存。我将异常追溯到std::vector的定义中,到

C++ std::sort 与类中的谓词函数

我想在特定类中以特定顺序对特定结构的vector进行排序。我在一个类中编写了结构和谓词函数的定义,并在具有这些结构和函数的类的方法中运行std::sort。但是出现了编译错误。gcc版本是4.0.1,操作系统是MacOSX。代码如下:classsample{public:structs{intx;inty;};boolcmp(structsa,structsb){if(a.x==b.x)returna.yvec;//...sort(vec.begin(),vec.end(),cmp);//compilationerror//...return0;}};intmain(void){sam

c++ - std::vector 如何支持未知大小的自定义对象的连续内存

我正在为正确的心智模型和对std::vector的理解而苦苦挣扎。我以为我知道的当您创建一个类型为T的vector,然后为该vector保留N个元素时,编译器基本上会找到并保留一个连续的内存块,即N*sizeof(T)字节。例如,//Initializeavectorofintstd::vectorintvec;//Reservecontigiousblockof44-bytechunksofmemoryintvec.reserve(4);//[|||]//Fillinginthememorychunkshasobviousbehavior:intvec.push_back(1);//

c++ - 尝试将 std::pair 插入 std::set

我不明白这段代码中的错误是什么:#include#include#includeusingnamespacestd;classA{public:A(unsignedinta):_a(a){}A():_a(0){}unsignedinta()const{return_a;}private:unsignedint_a;};classB{public:B(unsignedintb):_b(b){}B():_b(0){}unsignedintb()const{return_b;}private:unsignedint_b;};voiddisplay(constPoint&point){//co