C++ 字符串
C++ 字符串
字符串用于存储文本。
例如,"Hello World" 就是一个字符串。
字符串变量包含一系列由双引号包围的字符:
实例
创建一个类型为 string 的变量并为其赋值:
string greeting = "Hello";
要使用字符串,您必须在源代码中包含一个额外的头文件,即 <string> 库:
实例
// 包含 string 库 #include <string> // 创建一个字符串变量 string greeting = "Hello"; // 打印字符串 cout << greeting;
注意:字符串可以包含多个单词、空格和标点符号:
实例
string greeting = "Hello and welcome!"; cout << greeting;