c++ 打印输出
时间: 2023-06-28 14:11:20 浏览: 70
C++ 中可以使用 `std::cout` 来进行打印输出,例如:
```cpp
#include <iostream>
int main() {
std::cout << "Hello, world!" << std::endl;
return 0;
}
```
输出结果为:
```
Hello, world!
```
其中 `<<` 运算符表示将右侧的内容输出到左侧,`std::endl` 表示换行。
相关问题
c++打印输出沙漏图形
Sure, here's an example of how you can print a diamond pattern using asterisks in C:
```C
#include <stdio.h>
void printLine(int spaces, int stars) {
for (int i = 0; i < spaces; i++) {
printf(" ");
}
for (int i = 0; i < stars; i++) {
printf("*");
}
printf("\n");
}
void printDiamond(int n) {
int spaces = n-1;
int stars = 1;
// Print upper half
for (int i = 0; i < n; i++) {
printLine(spaces, stars);
spaces--;
stars += 2;
}
spaces = 1;
stars = n * 2 - 3;
// Print lower half
for (int i = 0; i < n-1; i++) {
printLine(spaces, stars);
spaces++;
stars -= 2;
}
}
int main() {
int n;
printf("Enter the number of rows: ");
scanf("%d", &n);
printDiamond(n);
return 0;
}
```
This program takes the number of rows as input and prints a diamond pattern accordingly. You can adjust the pattern by modifying the `printLine` and `printDiamond` functions.
c++ vector打印输出
C++中的vector是一个动态数组,可以存储任意类型的数据。要打印输出vector的内容,可以使用循环遍历vector的每个元素,并将其逐个输出。
下面是一个示例代码,演示了如何打印输出一个vector的内容:
```cpp
#include <iostream>
#include <vector>
int main() {
std::vector<int> numbers = {1, 2, 3, 4, 5};
// 使用for循环遍历vector并输出每个元素
for (int i = 0; i < numbers.size(); i++) {
std::cout << numbers[i] << " ";
}
std::cout << std::endl;
return 0;
}
```
运行以上代码,输出结果为:
```
1 2 3 4 5
```
你也可以使用迭代器来遍历vector并输出每个元素,示例如下:
```cpp
#include <iostream>
#include <vector>
int main() {
std::vector<int> numbers = {1, 2, 3, 4, 5};
// 使用迭代器遍历vector并输出每个元素
for (std::vector<int>::iterator it = numbers.begin(); it != numbers.end(); ++it) {
std::cout << *it << " ";
}
std::cout << std::endl;
return 0;
}
```
以上代码的输出结果与前面的示例相同。