istream之cin

xiaoxiao2021-02-27  199

#include <iostream> #include <string> using namespace std; void skipBlanks(istream &is) { while (is.peek() == '\n' || is.peek() == '\t' || is.peek() == ' ') is.ignore(); } int main() { //输入原理:程序的输入都建有一个缓冲区,即输入缓冲区。 //一次输入过程是这样的,当一次键盘输入结束时会将输入的数据存入输入缓冲区,而cin函数直接从输入缓冲区中取数据。 //正因为cin函数是直接从缓冲区取数据的,所以有时候当缓冲区中有残留数据时,cin函数会直接取得这些残留数据而不会请求键盘输入。 //char ch1, ch2; 输入方式一:cin.get();ch = cin.get()与cin.get(ch)等价 cin.get(ch1).get(ch2);//任意键符都会被读入(包括Tab键,空格键, 回车键),输入结束条件为回车键符,并且不会丢弃缓冲区的回车键符。 // 输入方式二:cin>> //cin >> ch1 >> ch2;//输入结束条件为回车键,并且丢弃缓冲区的空白键符(包括tab键,空格键, 回车键)。 //cout << "ch1:" << ch1 << ", ch2:" << ch2 << endl; char ch; string buf; while (!cin.eof()) { cin.get(ch); skipBlanks(cin); buf += ch; } cout << endl; cout << "You have input message:" << endl; cout << buf << ", size: " << buf.length() << endl; return 0; }
转载请注明原文地址: https://www.6miu.com/read-13488.html

最新回复(0)