c++字符串变量怎么定义

   2025-02-13 5120
核心提示:在C++中,可以使用std::string类定义字符串变量。需要包含头文件。以下是一个示例:#include iostream#include stringint main()

在C++中,可以使用std::string类定义字符串变量。需要包含头文件

以下是一个示例:

#include <iostream>#include <string>int main() {    std::string str = "Hello, World!"; // 定义并初始化字符串变量    std::cout << str << std::endl; // 输出字符串变量    return 0;}

在上述示例中,使用std::string类定义了一个名为str的字符串变量,并将其初始化为"Hello, World!"。然后使用std::cout输出该字符串变量。

注意需要引入头文件,并使用命名空间std::。

 
 
更多>同类维修知识
推荐图文
推荐维修知识
点击排行
网站首页  |  关于我们  |  联系方式  |  用户协议  |  隐私政策  |  网站留言