C++中string类size
阿神
阿神 2017-04-17 13:50:54
0
4
545

str.size() 输出结果不一样

str += 'a' + 'a';//报错

阿神
阿神

闭关修行中......

reply all(4)
迷茫

This is because: the former is done first'

And the latter appends two characters.

In fact, the output of the two should be:

ABCDa5
ABCDrrreeea6
Because

The reason for the new error you posted is that 'a' = 97

97 + 97 = 194

exceeds the range of 0-127 ascii characters, so it is implicitly converted from a numerical value to a character report warning

大家讲道理

This is a char type, not a constant string. First, according to the priority, the following '0'+'a' will be calculated first. For the char type, the addition is still a char type ('a'), so += Char type, only one character is actually appended.

    ¦ /**
    ¦  *  @brief  Append a character.
    ¦  *  @param c  The character to append.
    ¦  *  @return  Reference to this string.
    ¦  */
    ¦ basic_string&
    ¦ operator+=(_CharT __c)
    ¦ {
    this->push_back(__c);
    return *this;
    ¦ }
左手右手慢动作

Please do not send screenshots when sending codes.

This is because (1) the operator precedence is different, + is higher than +=; (2) the operands of addition are different.

s += '
s += ('
s = s + ('
char tmp = '
s += '
std::string tmp = s + 'rrreee';
s = tmp + 'a';
'; s += 'a';
' + 'a'; s += tmp;
' + 'a');
' + 'a');
' + 'a';

is equivalent to

rrreee

Similar to

rrreee

is equivalent to

rrreee

Adding two variables (characters) of type char is equivalent to adding the values ​​corresponding to the two characters, so '

黄舟

'0' + 'a' gets a new character, so this is equivalent to:

char c = 'rrreee' + 'a';
s += c; // 只是添加了一个字符
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template