草庐IT

c++ - 为什么我可以使用 const char* 作为 std::map<std::string, int> 中的键

我已经定义了一个数据结构std::mapa;我发现我可以像这样传递constchar*作为键:a["abc"]=1;哪个函数提供了从constchar*到std::string的自动类型转换? 最佳答案 std::string有一个constructorthatallowstheimplicitconversionfromconstchar*.basic_string(constCharT*s,constAllocator&alloc=Allocator());表示隐式转换,例如std::strings="Hello";是允许的。这

c++ - 在 for... 循环中使用 short 而不是 int 的好处

在for循环中使用short而不是int有什么好处吗?即for(shortj=0;j我99%的循环涉及3000以下的数字,所以我认为整数会浪费字节。谢谢! 最佳答案 不,没有任何好处。short最终可能会占用一个完整的寄存器(32位,一个int)。在IDE中输入额外的两个字母也会浪费时间。(那是个玩笑)。 关于c++-在for...循环中使用short而不是int的好处,我们在StackOverflow上找到一个类似的问题: https://stackover

c++ - Memset 一个 int(16 位)数组到 short 的最大值

似乎无法在任何地方找到这个问题的答案,如何将数组memset为数组类型的最大值?我原以为memset(ZBUFFER,0xFFFF,size)可以在ZBUFFER是16位整数数组的情况下工作。相反,我始终得到-1。此外,我们的想法是尽可能快地完成这项工作(它是一个需要初始化每一帧的zbuffer)所以如果有更好的方法(并且仍然一样快或更快),请告诉我。编辑:作为澄清,我确实需要一个带符号的int数组。 最佳答案 在C++中,您将使用std::fill和std::numeric_limits。#include#include#incl

c++ - c++中 "unsigned long int"的最大值

我如何知道“unsignedlongint”类型的变量的最大可赋值是多少? 最佳答案 显而易见的方法是使用std::numeric_limits::max(); 关于c++-c++中"unsignedlongint"的最大值,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/18301685/

c++ - 如何输入 int64_t/uint64_t 常量?

我想做的是定义一个等于2^30的常量(我可能会将其更改为2^34之类的东西,所以我更愿意为它设置一个大于32位的空间)。为什么以下最小(?)示例无法编译?#include//test.cpp:4:33:error:expectedprimary-expressionbeforenumericconstant//test.cpp:4:33:error:expected')'beforenumericconstantconstuint64_ttest=(uint64_t1) 最佳答案 您可以使用宏:UINT64_C为了定义64位无符号整

c++ - 为什么我不能像这样: int *p = 6;?直接给int指针赋值

错误:从“int”到“int*”的无效转换int*q=8;工作正常。*q=6;为什么我不能像这样直接将一个int赋值给一个int指针:int*q=6;我可以在下一行安全地分配它吗? 最佳答案 因为它们完全不同。第一个是带有初始化表达式的变量定义,即initialization(指针本身):int*q=8;~~~~~~~~~typeisint*;nameofvariableisq;initializedwith8第二个是赋值(指针指向的对象):*q=6;~~~~~dereferenceonqviaoperator*;assignthe

c++ - 为什么 "long *"和 "int *"在 32 位代码中不兼容?

我想知道为什么下面的代码不能编译:voidfoo_int(int*a){}voidfoo_long(long*a){}intmain(){inti;longl;foo_long(&i);foo_int(&l);}我使用的是GCC,在C或C++中都无法调用。由于是32位系统,int和long都是有符号的32位整数(可以在编译时用sizeof验证)。我问的原因是我有两个单独的头文件,它们都不在我的控制之下,一个做类似的事情:typedefunsignedlongu32;另一个:typedefunsignedintuint32_t;。声明基本上是兼容的,除了当我像上面的代码片段那样将它们用作

c++ - 为什么 lambda 没有从到达范围捕获类型 const double,而 const int 是?

我似乎无法理解为什么以下类型为constint的代码可以编译:intmain(){usingT=int;constTx=1;autolam=[](Tp){returnx+p;};}$clang++-clambda1.cpp-std=c++11$而这个类型为constdouble的不是:intmain(){usingT=double;constTx=1.0;autolam=[](Tp){returnx+p;};}$clang++-clambda2.cpp-std=c++11lambda1.cpp:5:32:error:variable'x'cannotbeimplicitlycaptur

c++ - 不区分大小写 unordered_map<string, int>

如何创建不区分大小写的unordered_map?是否覆盖key_equal足够了,否则我还需要更新hasher? 最佳答案 Hasher也需要更新,因为默认的哈希算法doesnotproduceidenticalhashcodeforstringsthatdifferonlyinthecaseoftheirsymbols-旨在处理不区分大小写的字符串的哈希码函数的一个基本属性。std::strings1="Hello";std::strings2="hello";std::hashhash_fn;size_thash1=hash_

C++ std::map<std::string, int> 获取键以特定字符串开头的值

我以这种方式使用std::map:#include#include#includeusingnamespacestd;intmain(intargc,char*argv[]){mapmy_map;my_map.insert(pair("Ab",1));my_map.insert(pair("Abb",2));my_map.insert(pair("Abc",3));my_map.insert(pair("Abd",4));my_map.insert(pair("Ac",5));my_map.insert(pair("Ad",5));coutsecondsecondhttp://ideo