参考: 面向对象程序设计及C++,P16
// excise_20230807a.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
using namespace std;
int main()
{
string s1; //定义空字符串s1
string s2 = "Student"; //定义字符串,并初始化
string s3 = s2;
string s4(8, 'A'); //初值是8个A
cout << "输入一个字符串,回车结束: " << endl;
cin >> s1; //读入字符串,遇到空格、tab、回车结束
cout << s1 << endl << s2 << endl << s3 << endl << s4<<endl;
s4 = s1;
cout << "s4 = " << s4 << "长度是:" << s4.length() << endl;
s2 = s3 + ' ' + s4; //连接字符串
cout << "s2 = " << s2 << "长度是:" << s2.length() << endl;
std::cout << "Hello World!\n";
getchar();
}
运行结果: