温馨提示×

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

c++
小亿
156
2023-12-09 11:13:24
栏目: 编程语言

在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::。

0