c++ print用法
在C++中,有多种方法可以输出内容到终端。以下是几种常见的打印输出方法:
1. 使用cout和<<运算符:
```cpp
#include <iostream>
int main() {
    std::cout << "Hello, World!" << std::endl;
    return 0;
}
```函数printf作用
在这个例子中,使用了iostream库中的cout对象来输出字符串"Hello, World!"。通过<<运算符可以连续输出多个内容。
2. 使用printf函数:
```cpp
#include <cstdio>
int main() {
    printf("Hello, World!\n");
    return 0;
}
```
与C语言中的用法类似,也可以使用printf函数来打印输出。需要包含头文件cstdio。
3. 使用puts函数:
```cpp
#include <cstdio>
int main() {
    const char* str = "Hello, World!";
    puts(str);
    return 0;
}
```
puts函数可以直接输出字符串。
这些只是C++中几种常见的打印输出方法,还有其他方法可根据具体需求选择。