目录

peek

描述 (Description)

它用来偷看下一个角色。

声明 (Declaration)

以下是std :: basic_istream :: peek的声明。

int_type peek();

参数 (Parameters)

没有

返回值 (Return Value)

返回输入序列中的下一个字符,而不提取它:将字符保留为要从流中提取的下一个字符。

异常 (Exceptions)

Basic guarantee - 如果抛出异常,则对象处于有效状态。

数据竞争 (Data races)

修改流对象。

例子 (Example)

在下面的示例中为std :: basic_istream :: peek。

#include <iostream>     
#include <string>       
#include <cctype>       
int main () {
   std::cout << "Please, enter a number or a word: ";
   std::cout.flush();    
   std::cin >> std::ws;  
   std::istream::int_type c;
   c = std::cin.peek();  
   if ( c == std::char_traits<char>::eof() )
   return 1;
   if ( std::isdigit(c) ) {
      int n;
      std::cin >> n;
      std::cout << "You entered the number: " << n << '\n';
   } else {
      std::string str;
      std::cin >> str;
      std::cout << "You entered the word: " << str << '\n';
   }
   return 0;
}

输出应该是这样的 -

Please, enter a number or a word: foobar
You entered the word: foobar
↑回到顶部↑
WIKI教程 @2018