字符串和字符串的串联

问题描述 投票:2回答:1
#include <bits/stdc++.h>
using namespace std;

int main() 
{
    string s1 = "Alice";
    s1 +='\0';
    s1 +='\0';
    cout<< s1.size()<<endl;         // output: 7

    string s2 = "Alex";
    s2 += "\0";
    s2 += "\0";
    cout<< s2.size()<<endl;         // output: 5
}

这里怎么了?请说明串联中单引号和双引号的作用之间的区别

c++
1个回答
4
投票
s1 +='\0';
不管字符是什么,都将字符添加到s1

s2 += "\0";

© www.soinside.com 2019 - 2024. All rights reserved.