C++;使用std::string行为进行字符连接。请解释一下 这里有一些关于C++ STD::字符串的例子,我不理解。
一, 四, 五, 在一个密码里,我有一行C++;使用std::string行为进行字符连接。请解释一下 这里有一些关于C++ STD::字符串的例子,我不理解。,c++,string,C++,String,一, 四, 五, 在一个密码里,我有一行 ans+=to_字符串(q)q是一个单位数整数。程序抛出运行时错误 将其更改为ans+=(q+'0')和错误已被删除 请帮我澄清这个想法 这是: std::string ans = ""+'a'; 不是您所想的,您实际上执行了与以下相同的操作: const char* p = ""; p = p + 97 /*97=='a'*/; // increase p pointer by `a` value, results in UB (pointer to
ans+=to_字符串(q)代码>q是一个单位数整数。程序抛出运行时错误
将其更改为ans+=(q+'0')代码>和错误已被删除
请帮我澄清这个想法 这是:
std::string ans = ""+'a';
不是您所想的,您实际上执行了与以下相同的操作:
const char* p = "";
p = p + 97 /*97=='a'*/; // increase p pointer by `a` value, results in UB (pointer to random memory)
std::string ans = p; // p points to possibly unallocated memory (UB).
这没什么意义
如果您使用clang编译它,您将得到一长串警告:
main.cpp:22:25: warning: adding 'char' to a string does not append to the string [-Wstring-plus-int]
std::string ans = ""+'a';
~~^~~~
main.cpp:22:25: note: use array indexing to silence this warning
std::string ans = ""+'a';
^
& [ ]
main.cpp:22:25: warning: adding 'char' to a string pointer does not append to the string [-Wstring-plus-char]
std::string ans = ""+'a';
~~^~~~
main.cpp:22:25: note: use array indexing to silence this warning
std::string ans = ""+'a';
^
& [ ]
“”是空字符串文本的地址a'被解释为一个整数,ASCII码65。这会将65添加到文本字符串的地址,这会导致未定义的行为,可能会导致崩溃
ans=ans+'a';
ans
是一个std::string
std::string
定义重载的+
运算符。实际上有好几个。其中一个特别是重载+
,其中参数是字符,并将字符附加到字符串中
ans = ans + (5 + '0'); // Throws error
5+'0'
是一个提升为int
类型的表达式std::string
不会以int
作为参数明确地重载+
运算符。这将导致编译错误
ans += (5 + '0'); //works
std::string
确实有一个明确的重载+=
运算符,因此这可以很好地编译。string literal是一个字符数组。它不是std::string
的实例。数组不能按值传递给函数或运算符,但当用作操作数时,数组会衰减为指向第一个字符的指针
字符是编码符号的数字。除'\0'
外,所有字符都具有非零值
在表达式“”+'a'
中,字符串文字衰减为指针,然后将'a'
字符表示为非零整数。此值将添加到指针。无论a
的值是多少(在常用的ASCII编码中它恰好是65),结果都超出了数组的界限。指针算术越界具有未定义的行为,输出为1。是未定义行为的结果
节目2。具有明确的和预期的行为
没有接受参数std::string
和int
的运算符+
。右边的参数是int
,因为5+'0'
中的char
参数升级为int
,因此两个参数的类型相同。这也是表达式的返回类型
这就是它变得多毛的地方。有一个操作符+
接受char
并且int
可转换为char
。但是,还有其他可能的转换是不明确的。下面是由clang显示的错误:
error: invalid operands to binary expression ('string' (aka 'basic_string<char>') and 'int')
ans = ans + (5 + '0'); // Throws error
~~~ ^ ~~~~~~~~~
./include/c++/6.1.0/bits/basic_string.h:4982:5: note: candidate template ignored: deduced conflicting types for parameter '_CharT' ('char' vs. 'int')
operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, _CharT __rhs)
^
./include/c++/6.1.0/bits/basic_string.h:5036:5: note: candidate template ignored: deduced conflicting types for parameter '_CharT' ('char' vs. 'int')
operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
^
这是因为operator+=(char)代码>是一个明确的重载
在一个代码中,我有一行ans+=to_字符串(q);q是一个一位数的整数。程序抛出运行时错误
“+”a“
将转到超出范围并调用未定义的行为。ans+=to_字符串(q)如果q
是整数,则code>应能正常工作。它如何超出范围?当+未重载时,向字符串添加字符是否有效?“
不是std::string
,它是一个char
数组,向其添加整数类型时,对该数组的地址执行指针算术。这与串联非常不同。在两边都是基本类型的情况下,您和其他任何人都不能重载运算符。所以,“
是(衰减为)一个字符常量*,'a'
是一个字符
,这意味着你要将字符a
的ASCII数添加到特定空字符串的地址中。。。因此越界并调用UB。但是,如果您想要错误期望的std::string
文本,那么请使用名称空间std::string\u文本执行;auto wow=“a std::string”s
。数组不能传递给函数或运算符,因为它们可以通过引用传递,@NathanOliver限定添加。在第5种情况下,5+'0'
也被提升为int
。那么,它与第四种情况有什么不同呢?不同的操作员+
与+=
相比,定义不同的重载。好的,所以+不是为int定义的,而+=是为int定义的。明白了。谢谢。不,+=
没有为int
定义。它只为char
定义,但是对于操作符+=
没有其他重载,int
参数可以转换为,因此没有歧义。C++11为使用移动语义的std::string
的右值引用添加了操作符+
。带有int
的运算符+
可以转换为基于常规副本的运算符+
,该运算符采用char参数,或者转换为基于移动的运算符+
,该运算符采用char参数,因此存在歧义。显然,操作符+=
没有移动语义。
string ans = ""+'a';
ans=ans+'a';
ans = ans + (5 + '0'); // Throws error
ans += (5 + '0'); //works
ans = ans + (5 + '0'); // Throws error
error: invalid operands to binary expression ('string' (aka 'basic_string<char>') and 'int')
ans = ans + (5 + '0'); // Throws error
~~~ ^ ~~~~~~~~~
./include/c++/6.1.0/bits/basic_string.h:4982:5: note: candidate template ignored: deduced conflicting types for parameter '_CharT' ('char' vs. 'int')
operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, _CharT __rhs)
^
./include/c++/6.1.0/bits/basic_string.h:5036:5: note: candidate template ignored: deduced conflicting types for parameter '_CharT' ('char' vs. 'int')
operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
^
ans += (5 + '0'); //works