C++ istream::peek()
功能:peek函数用于读取并返回下一个字符,但并不提取该字符到输入流中,也就是说,依然让该字符作为将要提取到输入流的下一个字符。
例程:
#include <iostream> #include <string> using namespace std; int main() { string word; char c; int n; cout << "Please enter a word or a number: "; c = cin.peek(); if(isdigit(c)) { cin >> n; cout << "You have entered a number: " << n << endl; } else { cin >> word; cout << "You have entered a word: " << word << endl; } return 0; }
peek()在文件流输入中依然适用。
更详细的信息:http://www.cplusplus.com/reference/istream/istream/peek/