unget()

来源:互联网 发布:linux dir命令 编辑:程序博客网 时间:2024/06/14 14:29

一次unget()调用会引起流后退一个位置,其本质是把最后一个字符读回到流中。
下面是使用unget()的情况,程序下如:
#include <iostream>
using namespace std;
int main() {
char ch1, ch2, ch3;
cin >> ch1 >> ch2 >> ch3;
cout << ch1 << ch2 << ch3 << endl;
cin.unget();
char firstChar, secondChar;
cin >> firstChar;
cout << firstChar << endl;
cin >> secondChar;
cout << secondChar << endl;
}
输入abcdef,结果则为:
abc
c
d
putback()就像unget()一样,允许在输入流中后退一个字符。二者的区别是,pubback()方法取流中要后退的字符作为参数,即:我们可以任意指定我们想要后退的一个字符,而不是最后一次就送入变量中的字符。
char ch1;
cin >> ch1;
cin.putback(ch1);
//ch1 will be the next character read off the stream

 

转自:http://blog.csdn.net/ruizema/article/details/6149662

0 0
原创粉丝点击