库:
<iostream>是Input Output Stream 的缩写,是标准的输入、输出流库,定义了标准的输入、输出对象。
输入:
std::cin是 istream 类的对象,其中c代表console,控制面板的意思,而in指输入,>>是流提取运算符
#include<iostream>
using namespace std;
int main() {
int a = 0;
cin >> a;
return 0;
}
输出:
std::cout是 ostream 类的对象,同理out指输出,<<是流插入运算符
#include<iostream>
using namespace std;
int main() {
int a = 0;
char b = 'a';
cin >> b;//如果输入两位数会出现什么情况呢?
cout << a << ' ' << b << endl;
return 0;
}
std::endl是⼀个函数,流插入输出时,相当于插入⼀个换行字符\n加刷新缓冲区。
C++的输入 输出可以自动识别变量类型(本质是通过函数重载实现的),其实最重要的是 C++的流能更好的支持自定义类型对象的输入输出。
这里没有包含<stdio.h>,也可以使用printf和scanf,在<iostream>包含间接包含了<stdio.h>。vs系列编译器是这样的,其他编译器可能会报错。