草庐IT

char_length

全部标签

c++ - 在 C++ 中命名 int 或 char 变量时, "n"或 "ch"前缀是常见前缀吗?

我目前正在浏览learncpp.com的C++教程,我看到他们的变量命名趋势是使用“n”前缀(即intnValue)和“ch”前缀为char变量(即charchOperation)命名int变量。这是不是我现在应该养成的行业普遍现象? 最佳答案 Isthissomethingthatiscommonplaceintheindustry?由于对somewhatmoreusefulconvention的误解,这种做法在二十或三十年前的Microsoft某些部门很常见。公司其他部分使用(标记变量表明它们的用途,在弱类型语言中,这有助于避免

c++ - operator const char* 以奇怪的方式覆盖(?)我的另一个变量

#include#includeclassVector{double_x;double_y;public:Vector(doublex,doubley):_x(x),_y(y){}doublegetX(){return_x;}doublegetY(){return_y;}operatorconstchar*(){std::ostringstreamos;os这个程序的输出:$./a.outVectorw1(1.1,2.2)Vectorw2(3.3,4.4)Vector(3.3,4.4)Vector(3.3,4.4)我不明白为什么会得到输出。似乎是“constchar*n2=w2;”覆盖

c++ - 如何让一个 char* 指向一个 char[]?

我想知道是否有办法让char*指向char数组的内容,这样我就可以修改char*跨功能。例如voidtoup(char*c){chararray[sizeof(c)];for(intx;x尝试使array=char*似乎不起作用。是否可以让char*指向char数组? 最佳答案 Isitpossibletomakethechar*pointtothechararray?是的。而不是:intmain(){char*c="Hello";toup(c);}使用:intmain(){charc[]="Hello";toup(c);}char

c++ - std::make_pair:无法将 'ch'(类型 'char')转换为类型 'char&&'

这个问题在这里已经有了答案:C++11make_pairwithspecifiedtemplateparametersdoesn'tcompile(1个回答)关闭9年前。以下代码有什么问题:#include#include#include#include#includeintmain(){std::vector>vec;for(unsignedi=0;i(ch,number))!=vec.end());std::cout(ch,number));}}它确实可以很好地编译:g++test.cxx但失败了:$g++-std=c++11test.cxx/tmptest.cxx:Infunct

c++ - const char * 和 char *

我明白了char*s="HelloWorld!";存储在只读内存中,不能通过指针修改字符串文字。这和有什么不同constchar*s="HelloWorld!";另外'string'的类型是char*还是constchar*? 最佳答案 区别在于后者合法,前者不合法。这是在C++11中所做的更改。形式上,"HelloWorld!"的类型为constchar[13];它可以转换为constchar*。在过去,它的类型可能是char[13],可以转换为char*。C++通过添加const更改了数组的类型,但保留了对char*的转换,以便

c++ - 为什么C++11中signed char的范围是-127到127?

这个问题在这里已经有了答案:Doestherangeforintegervaluesofachardependonimplementation?(5个答案)关闭4年前。我正在通读BjarneStroustrup的《C++编程语言第4版》,但我无法完全理解第141页中提到的以下段落。Eachcharacterhasanintegervalueinthecharactersetusedbytheimplementation.Forexample,thevalueof'b'is98intheASCIIcharacterset.Hereisaloopthatoutputsthetheinteg

c++ - 通过 "\0"而不是 memset() 批量初始化 char 数组

通常由memset初始化的字符数组。我在我的项目代码中发现了由"\0"初始化的char数组。我还编译并检查了,它工作正常。我的问题是这是批量初始化char数组的正确方法吗?例如:chara[20]="\0";printf("%s",a); 最佳答案 是的,这是正确的方法之一。对于c引用C11,第6.7.9章Iftherearefewerinitializersinabrace-enclosedlistthanthereareelementsormembersofanaggregate,orfewercharactersinastri

c++ - 如何将 LPCWSTR 转换为 const char *?

如何将LPCWSTR转换为constchar*?谢谢 最佳答案 使用WideCharToMultiByte功能。请注意,LPCWSTR与constwchar_t*相同,因此如果您在整个应用程序中处理宽字符数据,则可能不需要在全部。 关于c++-如何将LPCWSTR转换为constchar*?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4542521/

c++ - 将 char* 分配给字符串而不复制

这个问题在这里已经有了答案:initializingstd::stringfromchar*withoutcopy(6个答案)关闭9年前。这是一个非常简单的问题,但我发现它很棘手。我想把一个char*当作一个std::string,例如:char*p=...;//readahugechuckfromafilestd::strings(p);//thisisnotwhatIwant所以,如果我使用构造函数,我得到了p的拷贝,这是浪费内存和时间。是否有可能以某种方式避免这种情况,并将std::string内容“分配”到预先存在的地址?任何其他想法都非常受欢迎!谢谢!

c++ - 在 char 数组中搜索子字符串

这个问题在这里已经有了答案:Getasubstringofachar*[duplicate](5个答案)关闭9年前。据我所知,在C++字符串中有一个函数可以执行子字符串搜索:string1.find(string2)。有什么方法可以在char中执行相同的操作?下面是我的问题:#include#includeusingnamespacestd;intmain(){chara[]="helloworld!";charb[]="el";//findsubstringb[]ina[]}假设我需要在字符串中找到子字符串“el”,是否可以这样做?