草庐IT

c++ - 为什么在 C++ 中默认签名为 'char'?

为什么char在默认情况下应该在-128到127的范围内表示一个“字符”,其文本表示在0到255的范围内?从这个意义上说,我猜char默认情况下应该是无符号的,只有当我们打算只将它视为“数字”时,我们才必须添加“有符号”关键字。因此,我在处理文本文件时应该使用unsignedchar吗?我也不明白为什么std::ofstream的读写函数使用char而不是unsignedchar当我需要处理二进制文件时。我不在乎签名,对吗?此外,我使用signedchar成功制作了JPEG文件的拷贝。像这样://..openallstreams..charc;while(input.peek()!=E

c++ - char数组上的一元加运算符的目的是什么?

以下是做什么的?我认为+仅用于整数提升。charc[20]="hello";foo(+c);foo(+"hello"); 最佳答案 它强制数组衰减为指针,如§5.3.1[expr.unary.op]/7中间接说明的:Theoperandoftheunary+operatorshallhavearithmetic,unscopedenumeration,orpointertypeandtheresultisthevalueoftheargument.Integralpromotionisperformedonintegralorenu

c++ - char数组上的一元加运算符的目的是什么?

以下是做什么的?我认为+仅用于整数提升。charc[20]="hello";foo(+c);foo(+"hello"); 最佳答案 它强制数组衰减为指针,如§5.3.1[expr.unary.op]/7中间接说明的:Theoperandoftheunary+operatorshallhavearithmetic,unscopedenumeration,orpointertypeandtheresultisthevalueoftheargument.Integralpromotionisperformedonintegralorenu

c++ - new char[10] 和 new char(10) 有什么区别

在C++中,有什么区别char*a=newchar[10];和char*a=newchar(10);谢谢! 最佳答案 第一个分配10个字符的数组。第二个分配一个初始化为10的字符。或者:第一个应替换为std::vector,第二个应该放入智能指针中。 关于c++-newchar[10]和newchar(10)有什么区别,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/3902011

c++ - new char[10] 和 new char(10) 有什么区别

在C++中,有什么区别char*a=newchar[10];和char*a=newchar(10);谢谢! 最佳答案 第一个分配10个字符的数组。第二个分配一个初始化为10的字符。或者:第一个应替换为std::vector,第二个应该放入智能指针中。 关于c++-newchar[10]和newchar(10)有什么区别,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/3902011

c++ - const unsigned char * 到 std::string

sqlite3_column_text返回一个constunsignedchar*,如何将其转换为std::string?我试过std::string(),但我得到一个错误。代码:temp_doc.uuid=std::string(sqlite3_column_text(this->stmts.read_documents,0));错误:1>.\storage_manager.cpp(109):errorC2440:'':cannotconvertfrom'constunsignedchar*'to'std::string'1>Noconstructorcouldtakethesour

c++ - const unsigned char * 到 std::string

sqlite3_column_text返回一个constunsignedchar*,如何将其转换为std::string?我试过std::string(),但我得到一个错误。代码:temp_doc.uuid=std::string(sqlite3_column_text(this->stmts.read_documents,0));错误:1>.\storage_manager.cpp(109):errorC2440:'':cannotconvertfrom'constunsignedchar*'to'std::string'1>Noconstructorcouldtakethesour

c++ - 将int的所有字节都设置为(unsigned char)0,保证代表零?

Thisisnotamatterofrecommendedpractise(norundefinedbehavior),butaboutwhatthec++-standardactuallyguaranteesinthematterofturningallbytesofanintegertypetothevalueof(unsignedchar)0.问题在下面的片段中,if-statement使用的表达式保证在c++11中被评估为true?std::memset(reinterpret_cast(&a),//inta;(unsignedchar)0,sizeof(int));if(a=

c++ - 将int的所有字节都设置为(unsigned char)0,保证代表零?

Thisisnotamatterofrecommendedpractise(norundefinedbehavior),butaboutwhatthec++-standardactuallyguaranteesinthematterofturningallbytesofanintegertypetothevalueof(unsignedchar)0.问题在下面的片段中,if-statement使用的表达式保证在c++11中被评估为true?std::memset(reinterpret_cast(&a),//inta;(unsignedchar)0,sizeof(int));if(a=

c++ - 为什么 C 或 C++ 标准不明确将 char 定义为有符号或无符号?

intmain(){charc=0xff;boolb=0xff==c;//UndermostC/C++compilers'defaultoptions,bisFALSE!!!}C或C++标准都没有将char指定为有符号或无符号,它是实现定义的。为什么C/C++标准没有明确将char定义为有符号或无符号,以避免像上面的代码这样的危险误用? 最佳答案 主要是历史原因。char类型的表达式在大多数情况下都被提升为int(因为很多CPU没有8位算术运算)。在某些系统上,符号扩展是执行此操作的最有效方式,它主张使普通char签名。另一方面,E