jjzjj

stdstring

全部标签

c++ - GCC 4.x/C++11 中的 std::string 引用计数了吗?

当使用带有-std=c++0x或-std=c++11std::string是否被引用计数? 最佳答案 看libstdc++documentation我发现(查看链接了解更多信息):字符串如下所示:[_Rep]_M_length[basic_string]_M_capacity_M_dataplus_M_refcount_M_p---------------->unnamedarrayofchar_type所以,是的,它是ref计数的。另外,来自讨论here:Yes,std::stringwillbemadenon-reference

c++ - std::string 可以包含嵌入的空值吗?

对于常规C字符串,空字符'\0'表示数据结束。std::string怎么样,我可以有一个嵌入空字符的字符串吗? 最佳答案 是的,您可以在std::string中嵌入空值。例子:std::strings;s.push_back('\0');s.push_back('a');assert(s.length()==2);注意:std::string的c_str()成员总是会在返回的字符缓冲区中附加一个空字符;但是,std::string的data()成员可能会也可能不会将空字符附加到返回的字符缓冲区。小心操作符+=需要注意的一点是不要在R

c++ - std::string 可以包含嵌入的空值吗?

对于常规C字符串,空字符'\0'表示数据结束。std::string怎么样,我可以有一个嵌入空字符的字符串吗? 最佳答案 是的,您可以在std::string中嵌入空值。例子:std::strings;s.push_back('\0');s.push_back('a');assert(s.length()==2);注意:std::string的c_str()成员总是会在返回的字符缓冲区中附加一个空字符;但是,std::string的data()成员可能会也可能不会将空字符附加到返回的字符缓冲区。小心操作符+=需要注意的一点是不要在R

c++ - std::string += 运算符不能将 0 作为参数传递

std::stringtmp;tmp+=0;//compileerror:ambiguousoverloadfor'operator+='(operandtypesare'std::__cxx11::string{akastd::__cxx11::basic_string}'and'int')tmp+=1;//oktmp+='\0';//ok...expectedtmp+=INT_MAX;//oktmp+=int(INT_MAX);//stillok...what?第一个认为传递整数作为参数,对吗?为什么其他人通过编译?我在VisualC++和g++上测试过,我得到了与上面相同的结果。

c++ - std::string += 运算符不能将 0 作为参数传递

std::stringtmp;tmp+=0;//compileerror:ambiguousoverloadfor'operator+='(operandtypesare'std::__cxx11::string{akastd::__cxx11::basic_string}'and'int')tmp+=1;//oktmp+='\0';//ok...expectedtmp+=INT_MAX;//oktmp+=int(INT_MAX);//stillok...what?第一个认为传递整数作为参数,对吗?为什么其他人通过编译?我在VisualC++和g++上测试过,我得到了与上面相同的结果。

c++ - std::atomic<std::string> 是否正常工作?

我正在阅读AnthonyWilliams的“C++ConcurrencyinAction”和第5章,其中讨论了新的多线程感知内存模型和原子操作,他说:Inordertousestd::atomicforsomeuser-definedUDT,thistypemusthaveatrivialcopyassignmentoperator.据我了解,这意味着我们可以使用std::atomic如果以下返回true:std::is_trivially_copyable::value按照这个逻辑,我们应该不能使用std::string作为std::atomic的模板参数并让它正常工作。但是,以下代

c++ - std::atomic<std::string> 是否正常工作?

我正在阅读AnthonyWilliams的“C++ConcurrencyinAction”和第5章,其中讨论了新的多线程感知内存模型和原子操作,他说:Inordertousestd::atomicforsomeuser-definedUDT,thistypemusthaveatrivialcopyassignmentoperator.据我了解,这意味着我们可以使用std::atomic如果以下返回true:std::is_trivially_copyable::value按照这个逻辑,我们应该不能使用std::string作为std::atomic的模板参数并让它正常工作。但是,以下代

c++ - 为什么编译器更喜欢 f(const void*) 而不是 f(const std::string &)?

考虑以下代码:#include#include//voidf(constchar*){std::cout我使用g++(Ubuntu6.5.0-1ubuntu1~16.04)6.5.020181026编译了这个程序:$g++-std=c++11strings_1.cpp-Wall$./a.outconstvoid*请注意,注释是为了测试而存在的,否则编译器会使用f(constchar*)。那么,为什么编译器会选择f(constvoid*)而不是f(conststd::string&)? 最佳答案 转换为std::string需要“用户

c++ - 为什么编译器更喜欢 f(const void*) 而不是 f(const std::string &)?

考虑以下代码:#include#include//voidf(constchar*){std::cout我使用g++(Ubuntu6.5.0-1ubuntu1~16.04)6.5.020181026编译了这个程序:$g++-std=c++11strings_1.cpp-Wall$./a.outconstvoid*请注意,注释是为了测试而存在的,否则编译器会使用f(constchar*)。那么,为什么编译器会选择f(constvoid*)而不是f(conststd::string&)? 最佳答案 转换为std::string需要“用户

c++ - 错误 : invalid operands of types ‘const char [35]’ and ‘const char [2]’ to binary ‘operator+’

在我的文件顶部#defineAGE"42"稍后在文件中我多次使用ID,包括一些看起来像的行std::stringname="Obama";std::stringstr="Hello"+name+"youare"+AGE+"yearsold!";str+="Doyoufeel"+AGE+"yearsold?";我得到错误:"error:invalidoperandsoftypes‘constchar[35]’and‘constchar[2]’tobinary‘operator+’"在第3行。我做了一些研究,发现这是因为C++如何处理不同的字符串,并且能够通过将“AGE”更改为“strin