简体   繁体   English

字符串运算符 +(重载)char 数组和 string.in cpp 的不同行为

[英]string operator + (overloaded) different behavior for char array and string.in cpp

I tried merging 2 arrays using string operator.我尝试使用字符串运算符合并 2 arrays。 Following case works fine, + operator accepts char* and string.以下案例工作正常,+ 运算符接受 char* 和字符串。

    string s = "";;
    char a[5] = { 'f', 'i', 'r', 's', 't' };
    char b[6] = { 's', 'e', 'c', 'o', 'n', 'd' };
    s = a + string(b);

But this case does not work fine when I pass char* and string.但是当我传递 char* 和 string 时,这种情况不能正常工作。 I was confused why is it so?我很困惑为什么会这样?

    string s = "";;
    char a[5] = { 'f', 'i', 'r', 's', 't' };
    char b[6] = { 's', 'e', 'c', 'o', 'n', 'd' };
    s = a + "{";

could someone please explain.有人可以解释一下吗?

Because "{" is not std::string , but const char[2] (including the null terminator char) which could decay to const char* .因为"{"不是std::string ,而是const char[2] (包括 null 终止符字符),它可能衰减为const char* Then a + "{" is just pointer arithmetic (addition) which is invalid.然后a + "{"只是无效的指针算术(加法)。

You need to change either of the operand to std::string to make the operator+ for std::string to be called.您需要将任一操作数更改为std::string以使 std::string 的std::string operator+被调用。 eg例如

a + std::string("{");

or use literals (since C++14).或使用文字(C++14 起)。

a + "{"s;

C-style strings have a nul terminator ( '\0' ) at the end. C 风格的字符串末尾有一个 nul 终止符 ( '\0' )。 Library code that handles them uses the nul terminator to find the end of the string.处理它们的库代码使用 nul 终止符来查找字符串的结尾。

const char *a = "abc";

Here, "abc" is an array of 4 char , as if you had written在这里, "abc"是一个 4 char的数组,就像你写的一样

const char *a = { 'a', 'b', 'c', '\0' );

If you leave out the '\0' the library code won't know that you are interested only in the three characters that you put in the initializer.如果您省略了'\0' ,库代码将不会知道您只对放入初始化程序中的三个字符感兴趣。 It simply won't work right.它根本无法正常工作。 Formally, the behavior is undefined;形式上,行为是未定义的; anything the program does is legal.该程序所做的任何事情都是合法的。

To make this code work right, add a nul terminator to each of the C-style strings:要使此代码正常工作,请为每个 C 样式字符串添加一个 nul 终止符:

char a[] = { 'f', 'i', 'r', 's', 't', '\0' };
char b[] = { 's', 'e', 'c', 'o', 'n', 'd', '\0' };

Note that I removed the array size from a[5] and b[6] .请注意,我从a[5]b[6]中删除了数组大小。 The compiler will figure it out from the initializer.编译器将从初始化程序中找出它。 The type of a is "array of 6 char ", and the type of b is "array of 7 char ". a的类型是“6 个char的数组”, b的类型是“7 个char的数组”。

The second problem, even after this is fixed, is that第二个问题,即使在这个问题得到解决之后,是

std::string s = a + "{"

doesn't do what it looks like.不做它看起来的样子。 "{" is a C-style string (ie, an array of char ). "{"是 C 风格的字符串(即char数组)。 There is no + operator for two C-style strings.两个 C 风格的字符串没有+运算符。 To concatenate two C-style strings into an object of type std::string you can either do two separate operations:要将两个 C 风格的字符串连接到std::string类型的 object 中,您可以执行两个单独的操作:

std::string s = a;
s += "{";

or you can explicitly convert one (or both) of the C-style strings to std::string :或者您可以将一个(或两个)C 样式字符串显式转换为std::string

std::string s = std::string(a) + "{";

I generally prefer the first approach, but the second is certainly reasonable.我通常更喜欢第一种方法,但第二种方法当然是合理的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM