#include <iostream>
#include <string>
int main() {
// C++ 中的字符串类型是 std::string,它位于 <string> 头文件中。
// 创建一个空字符串
std::string emptyString;
std::cout << "Empty string: '" << emptyString << "'" << std::endl;
// 使用字符串字面量初始化字符串
std::string hello = "Hello, World!";
std::cout << "Initialized with a string literal: " << hello << std::endl;
// 使用构造函数初始化字符串
std::string constructedString(10, 'a'); // 构造包含 10 个 'a' 的字符串
std::cout << "Constructed with 10 'a's: " << constructedString << std::endl;
// 字符串拼接
std::string firstPart = "Hello";
std::string secondPart = "World";
std::string combined = firstPart + ", " + secondPart + "!";
std::cout << "Concatenated string: " << combined << std::endl;
// 获取字符串长度
std::cout << "Length of combined string: " << combined.length() << std::endl;
// 访问字符串中的单个字符
std::cout << "First character of combined string: " << combined[0] << std::endl;
// 修改字符串中的单个字符
combined[0] = 'J';
std::cout << "Modified combined string: " << combined << std::endl;
return 0;
}
std::string
是 C++ 标准库提供的字符串类,位于 <string>
头文件中。上一篇:c++ map添加元素的方法
下一篇:c++ dll
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站