在 c++ 中输出 string 类型有以下方法:使用 std::cout 对象:std::cout

C++ 中输出 string 类型
在 C++ 中,您可以使用以下方法来输出 string 类型:
-
std::cout 对象:
std::cout
-
输出运算符(
立即学习“C++免费学习笔记(深入)”;
string_variable
示例:
<code class="cpp">#include <iostream>
#include <string>
using namespace std;
int main() {
string my_string = "Hello, C++!";
// 使用 std::cout 对象输出
cout << my_string << endl;
// 使用输出运算符输出
my_string << cout << endl;
return 0;
}</code>输出:
<code>Hello, C++! Hello, C++!</code>
注意:
- 这两种方法本质上是等效的,但使用
std::cout对象输出更常见。 -
endl操纵符可用于换行。











