在 C++ 中,我们通过调用输入输出流库中的流对象 cin 和 cout 来实现输入和输出。
#includeusing namespace std;int main(){ int a = 0; float b = 0; char c = 0; cin >> a >> b >> c; cout << a << '\t' << b << '\t' << c << '\t' << endl; return 0;}
56 Enter5.36 Entera Enter56 5.36 a
在用 cin 进行输入时,我们不用指定具体的数据类型,系统会根据变量的类型从输入流中提取相应长度的字节。同样,用 cout 进行输出时,系统也会自动判别输出数据的类型使输出的数据按相应的类型输出。
通过 cin 读入数据时,会自动跳过输入流中的空格、tab 键和换行字符。当遇到无效字符或者文件结束符时,输入 cin 就会处于出错状态,我们可以通过判断 cin 的值来判断流对象是否处于正常状态和提取操作是否成功。
另外,我们还可以使用控制符来设置输入输出的格式。
#include#include using namespace std;int main(){ int a = 256; float b = 3.1415926525; cout << b << endl; cout << setprecision(9) << b << endl; // 设置精度 cout << setiosflags(ios::fixed) << setprecision(2) << b << endl; // 设置小数点位数 cout << endl << a << endl; cout << hex << a << endl; // 十六进制输出 cout << dec << setfill('*') << setw(5) << a << endl; // 设置域宽和填充 cout << setiosflags(ios::showpos) << a << endl; // 显示符号 cout << resetiosflags(ios::showpos) << a << endl; // 取消显示符号 return 0;}
3.141593.141592743.24256100**256+256256
也可以使用 cout 的成员函数来设置输入输出的格式。
#includeusing namespace std;int main(){ int a = 256; float b = 3.1415926525; cout << b << endl; cout.precision(9); // 设置精度 cout << b << endl; cout.precision(2); cout.setf(ios::fixed); // 设置固定小数点位数 cout << b << endl; cout << endl << a << endl; cout.setf(ios::hex, ios::basefield); cout << a << endl; cout.fill('*'); // 设置填充 cout.width(5); // 设置域宽 cout << a << endl; cout.setf(ios::showpos); // 显示符号 cout << a << endl; cout.unsetf(ios::showpos); // 取消显示符号 cout << a << endl; return 0;}
3.141593.141592743.24256100**256+256256
为了与 C 语言兼容,C++ 保留了 C 中的 scanf 和 printf 函数进行输入输出,以及 getchar 和 putchar 函数进行单个字符的输入输出。
此外,我们还可以用输入输出流对象的一些成员函数来实现输入和输出。
-
cout.put()
输出单个字符,可以连续输出 -
cin.get()
读入一个字符(包括空白字符),返回读入成功的字符,如遇到文件结束符,返回 EOF -
cin.get(ch)
读入一个字符并赋值给变量 ch,成功读入则返回真 -
cin.get
(字符数组或指针,字符个数 n,终止字符) 读入 n-1 个字符,如遇到终止字符则提前结束 -
cin.getline
(字符数组或指针,字符个数 n,终止字符) 与上面的 cin.get 类似,但是遇到终止字符时,字符指针会移到该终止字符后面,而 cin.get 则会停留在原位置
#includeusing namespace std;int main(){ char ch[20] = {0}; cin >> ch[0]; // 读入 1 cout.put(ch[0]); cout << endl; ch[1] = cin.get(); // 读入 2 cout.put(ch[1]); cout << endl; cin.get(ch[2]); // 读入 3 cout.put(ch[2]); cout << endl; cin.get(ch, 20, '/'); // 读入 123 之后的第一个回车以及 'hello, seniusen! h' 共计 19 个字符 cout << ch << endl; cin.get(ch, 20, '/'); // 读入'ello, seniusen!' 共计 15 个字符,遇到终止字符 ‘/’ 停止 cout << ch << endl; cin.getline(ch, 20, '/'); // 当前字符指针还停留在字符 ‘/’ 处,直接停止读入,字符指针后移一位指向空格 cout << ch << endl; cin.getline(ch, 20, '/'); // 读入' hello, seniusen!' 共计 17 个字符,遇到终止字符 ‘/’ 停止 cout << ch << endl; return 0;}
123 Enter123hello, seniusen! hello, seniusen!/ hello, seniusen!/ Enterhello, seniusen! hello, seniusen! hello, seniusen!
一些其他的成员函数:
-
cin.eof()
如果到达文件末尾(遇文件终止符)返回真,否则返回假 -
cin.peek()
返回当前指针指向的字符,但只是观测,指针仍然停留在当前位置 -
cin.putback(ch)
将字符 ch 返回到输入流,插入到当前指针位置 -
cin.ignore
(n, 终止字符) 跳过输入流中 n 个字符,若遇到终止符提前结束,此时指向终止字符后面一个位置
获取更多精彩,请关注「seniusen」!
![](https://upload-images.jianshu.io/upload_images/11895466-ee82f7655f20bfeb.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)