📅  最后修改于: 2023-12-03 14:59:49.489000             🧑  作者: Mango
C++ 是一种非常流行的编程语言,拥有广泛的应用范围。其中,输出是 C++ 编程的重要组成部分。下面,我们将介绍 C++ 的输出相关知识,并提供 35 套代码示例,帮助程序员快速入门。
在 C++ 中,输出是通过流(stream)的方式实现的。C++ 标准库提供了多种输出流,包括:
cout
:标准输出流,可以将输出信息显示在控制台上。cerr
:错误输出流,用于输出程序运行时的错误信息。clog
:日志输出流,用于输出程序运行时的日志信息。这些流都是标准输出流,也就是说它们都是与控制台相关联的输出流。除此之外,还可以使用文件输出流将信息输出到指定的文件中。
在 C++ 中,输出是通过输出操作符 <<
来实现的。C++ 标准库支持多种类型的输出操作符,例如:
<<
:输出字符、字符串、数字等基本类型数据。<< std::endl
:输出换行符,相当于 "\n"
。<< std::setw(n)
:输出指定宽度(n)的字符或字符串。<< std::hex
:输出 16 进制格式的数字。<< std::oct
:输出 8 进制格式的数字。<< std::setprecision(n)
:输出指定精度(n)的浮点数。下面是 35 个 C++ 输出的示例,每个示例均使用 C++ 标准库提供的输出流和输出操作符。
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
输出:
Hello, World!
#include <iostream>
int main() {
std::cout << 123 << std::endl;
return 0;
}
输出:
123
#include <iostream>
int main() {
std::cout << 3.14 << std::endl;
return 0;
}
输出:
3.14
#include <iostream>
int main() {
std::cout << 'A' << std::endl;
return 0;
}
输出:
A
#include <iostream>
int main() {
bool flag = true;
std::cout << flag << std::endl;
return 0;
}
输出:
1
#include <iostream>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
for (int i = 0; i < 5; i++) {
std::cout << arr[i] << " ";
}
std::cout << std::endl;
return 0;
}
输出:
1 2 3 4 5
#include <iostream>
int main() {
char str[6] = "hello";
for (int i = 0; i < 6; i++) {
std::cout << str[i];
}
std::cout << std::endl;
return 0;
}
输出:
hello
#include <iostream>
int main() {
int num = 10;
int *p = #
std::cout << p << std::endl;
return 0;
}
输出:
0x7ffee81d78d4
#include <iostream>
int main() {
int num = 10;
int *p = #
std::cout << *p << std::endl;
return 0;
}
输出:
10
#include <iostream>
#include <iomanip>
int main() {
std::cout << std::setw(10) << "hello" << std::endl;
return 0;
}
输出:
hello
#include <iostream>
#include <iomanip>
int main() {
std::cout << std::hex << 255 << std::endl;
return 0;
}
输出:
ff
#include <iostream>
#include <iomanip>
int main() {
std::cout << std::oct << 255 << std::endl;
return 0;
}
输出:
377
#include <iostream>
#include <iomanip>
int main() {
std::cout << std::setprecision(4) << 3.14159 << std::endl;
return 0;
}
输出:
3.142
#include <iostream>
int main() {
int a = 1;
int b = 2;
std::cout << "a = " << a << ", b = " << b << std::endl;
return 0;
}
输出:
a = 1, b = 2
#include <iostream>
int main() {
int num = 123;
std::cout << "0x" << std::hex << num << std::endl;
return 0;
}
输出:
0x7b
#include <iostream>
int main() {
std::cout << "line1" << std::endl;
std::cout << "line2" << std::endl;
std::cout << "line3" << std::endl;
return 0;
}
输出:
line1
line2
line3
#include <iostream>
int main() {
std::cout << "line1\nline2\nline3" << std::endl;
return 0;
}
输出:
line1
line2
line3
#include <iostream>
int main() {
std::cout << "column1\tcolumn2\tcolumn3" << std::endl;
return 0;
}
输出:
column1 column2 column3
#include <iostream>
int main() {
std::cout << "backspace:\b" << std::endl;
return 0;
}
输出:
backspace:
#include <iostream>
int main() {
std::cout << "line1";
std::cout << "line2";
std::cout << "line3" << std::endl;
return 0;
}
输出:
line1line2line3
#include <iostream>
int main() {
std::cerr << "Error: something went wrong!" << std::endl;
return 0;
}
输出:
Error: something went wrong!
#include <iostream>
int main() {
std::clog << "Log: the program is running normally." << std::endl;
return 0;
}
输出:
Log: the program is running normally.
#include <iostream>
#include <iomanip>
int main() {
std::cout << std::setw(10) << std::setfill('*') << "" << std::endl;
return 0;
}
输出:
**********
#include <iostream>
#include <fstream>
int main() {
std::ofstream outfile;
outfile.open("out.txt");
for (int i = 0; i < 10; i++) {
std::cout << "#";
outfile << "#";
}
std::cout << std::endl;
outfile << std::endl;
outfile.close();
return 0;
}
输出:
##########
#include <iostream>
int main() {
int a = 10;
int b = 20;
std::cout << "a + b = " << a + b << std::endl;
return 0;
}
输出:
a + b = 30
#include <iostream>
int main() {
int a = 20;
int b = 10;
std::cout << "a - b = " << a - b << std::endl;
return 0;
}
输出:
a - b = 10
#include <iostream>
int main() {
int a = 10;
int b = 20;
std::cout << "a * b = " << a * b << std::endl;
return 0;
}
输出:
a * b = 200
#include <iostream>
int main() {
int a = 20;
int b = 10;
std::cout << "a / b = " << a / b << std::endl;
return 0;
}
输出:
a / b = 2
#include <iostream>
int main() {
int a = 20;
int b = 10;
std::cout << "a % b = " << a % b << std::endl;
return 0;
}
输出:
a % b = 0
#include <iostream>
int main() {
int num = 10;
int *p = #
std::cout << &num << std::endl;
std::cout << p << std::endl;
return 0;
}
输出:
0x7ffee81d78d4
0x7ffee81d78d4
#include <iostream>
int main() {
char str[6] = "hello";
std::cout << str << std::endl;
return 0;
}
输出:
hello
#include <iostream>
int main() {
char str[6] = "hello";
std::cout.write(str, 3);
std::cout << std::endl;
return 0;
}
输出:
hel
#include <iostream>
#include <bitset>
int main() {
std::bitset<8> b(42);
std::cout << b << std::endl;
return 0;
}
输出:
00101010
#include <iostream>
#include <iomanip>
int main() {
int num = 42;
std::cout << std::dec << num << std::endl;
std::cout << std::hex << num << std::endl;
std::cout << std::oct << num << std::endl;
return 0;
}
输出:
42
2a
52
#include <iostream>
#include <fstream>
int main() {
std::ifstream infile;
infile.open("input.txt");
char c;
while (infile.get(c)) {
std::cout << c;
}
infile.close();
return 0;
}
假设文件 input.txt
中的内容为:
hello, world!
则输出:
hello, world!
至此,我们介绍了 C++ 输出相关的知识,并提供了 35 个实用的代码示例,希望能够帮助程序员们更好地理解和应用 C++ 的输出功能。