第二节 · 可供理解的数据 · 字符串库
char str[] = "Hello";#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1; // 空字符串
string s2 = "Hello"; // 用字符串字面量初始化
string s3("World"); // 另一种初始化方式
string s4(5, 'A'); // 5 个 'A',即 "AAAAA"
string s5 = s2; // 复制 s2
string s6 = s2 + " " + s3; // 字符串拼接
cout << s6 << endl; // 输出: Hello World
return 0;
}习题
Last updated