在本教程中,我们将借助示例学习如何使用 cin 对象获取用户输入以及使用 cout 对象向用户显示输出。
C++ 的 I/O 操作使用了流的概念。字节序列或数据流被称为流。它将加快性能。
当字节从主内存流向打印机、显示屏或网络连接等设备时,这被称为输出操作。
当字节从打印机、显示屏或网络连接等设备流向主内存时,会发生输入操作。
让我们来看一些C++编程中最常用的头文件
| 头文件 | 函数及描述 |
|---|---|
<iostream> |
它主要用于定义 cout、cin 和 cerr 对象,它们分别对应标准输出流、标准输入流和标准错误流。 |
<iomanip> |
它用于声明诸如 setprecision 和 setw 等服务,这些服务对于执行格式化 I/O 非常有用。 |
<fstream> |
它用于声明用户控制的文件处理服务。 |
C++中的 cout 将格式化的输出发送到标准输出设备,如屏幕。cout 对象和 << 运算符用于输出显示。
#include <iostream>
using namespace std;
int main() {
// prints the string which should be enclosed in double quotes
cout << "This is learn eTutorials";
return 0;
}
输出
This is learn eTutorials
iostream 头文件,这使我们能够显示输出。cout 对象定义在 std 命名空间内。我们使用了 `using namespace std;` 语句来使用 std 命名空间。cout 是一个打印用引号括起来的字符串 " " 的对象。`return 0;` 是 main() 函数的“退出状态”,紧跟在运算符之后。程序以该语句结束,但它不被认为是必需的。cout。
#include <iostream>
int main() {
//always prints the string that should be enclosed in double quotes
std::cout << "This is an amazing method";
return 0;
}
输出
This is an amazing method
cout 对象来打印数字和字符变量,但没有引号。
#include <iostream>
using namespace std;
int main() {
int num1 = 40;
double num2 = 125.703;
char ch = 'A';
cout << num1 << endl; // print integer
cout << num2 << endl; // print double
cout << "character: " << ch << endl; // print char
return 0;
}
输出
40 125.703 character: A
使用 endl 操作符插入新行。因此,每个输出都显示在新的一行上。
如果我们想在单个语句中打印多个变量、字符串等,我们可以多次使用该运算符。
cout << "character: " << ch << endl;
cin 接受来自标准输入设备(如键盘)的格式化输入。我们使用 cin 对象和 >> 运算符来获取输入。
#include <iostream>
using namespace std;
int main() {
int num;
cout << " please enter an integer and wait: ";
cin >> num; // Taking input
cout << "The number is: " << num;
return 0;
}
输出
please enter an integer and wait: 3 The number is: 3
在程序中,我们使用 cin >> num; 来收集用户输入。变量 num 存储输入。我们使用 >> 运算符和 cin 来接受输入。
如果未包含 using namespace std; 语句,我们必须使用 std::cin 代替 cin。
#include <iostream>
using namespace std;
int main() {
char a;
int num;
cout << " please enter a character and an integer with a smile: ";
cin >> a >> num;
cout << "Character: " << a << endl;
cout << "Number: " << num;
return 0;
}
输出
please enter a character and an integer with a smile: T 100 Character: T Number: 100
Cerr 是一个预定义的对象,是 iostream 类的一个实例。标准错误设备,也作为显示屏,据说附加到 cerr 对象。然而,对象 cerr 是无缓冲的,因此每次向 cerr 插入流都会使其输出立即显示。
如下例所示,cerr 也与流插入运算符一起使用。
#include <iostream>
using namespace std;
int main() {
char str[] = "not able to read....";
cerr << "Error message : " << str << endl;
}
输出
Error message : not able to read....
clog 是 ostream 类的一个预定义对象实例。标准错误设备,同时也是一个显示屏,据说附加到 clog 对象上,但 clog 对象是带缓冲的。这意味着每次插入 clog 的内容可能会被保存在缓冲区中,直到缓冲区被填满或刷新。
如下例所示,clog 也与流插入运算符一起使用。
#include <iostream>
using namespace std;
int main() {
char str[] = " not able to read....";
clog << "Error message detected : " << str << endl;
}
输出
Error message detected : not able to read....
通过这些小例子,你可能不会注意到 cout、cerr 或 clog 之间的任何区别,但在编写和运行大型程序时,这种差异会变得更加明显。因此,在显示错误消息时使用 cerr 流,在显示其他日志消息时使用 clog。
在竞赛编程中,大多数时候我们必须手动输入来检查我们的代码。然而,如果我们遇到像图、字符串或大量输入数据录入这样的问题,肯定会超时,或者输入错误数据的可能性会增加。
我们可以通过简单地将测试用例保存到所需位置的指定文件中来轻松解决问题。这在像 Facebook Hackercup 和 Google Codejam 这样的比赛中很有用。这些比赛不提供在线评测环境;相反,你必须上传输入和输出文件。
我们主要使用这两种文件访问模式进行输入/输出,即:
对于标准输入和输出,我们可以在 C/C++ 中使用 freopen。该函数的语法如下
语法
FILE * freopen(const char * filename, const char * mode, FILE * stream );
filename:这是我们想要打开的文件的名称。
mode:如前所述(使用这两种文件访问模式进行输入/输出,即 r 和 w)
stream:指向 FILE 对象的指针,该对象标识需要重新打开的流。