C++ basic_ios 库 - 运算符>>


描述

它用于提取格式化输入。

应用于输入流的该运算符 (>>) 称为提取运算符 -

  • 算术类型- 从流中按顺序提取和解析字符,将它们解释为正确类型的值的表示,该值存储为 val 的值。在内部,该函数通过首先构造一个哨兵对象(将 noskipws 设置为 false)来访问输入序列。然后(如果好的话),它调用 num_get::get (使用流的选定区域设置)来执行提取和解析操作,并相应地调整内部状态标志。最后,它在返回之前销毁哨兵对象。

  • 流缓冲区- 从流中提取尽可能多的字符并将它们插入到由 sb 指向的流缓冲区对象控制的输出序列中(如果有),直到输入序列耗尽或函数无法插入到指向的对象中由某人

  • 操纵器- 调用 pf(*this),其中 pf 可以是操纵器。操纵器是专门设计用于与该运算符一起使用时调用的函数。此操作对输入序列没有影响,并且不会提取任何字符(除非操纵器本身这样做,例如 ws)。

宣言

以下是 std::basic_istream::operator>> 的声明

C++98

arithmetic types (1)	
basic_istream& operator>> (bool& val);
basic_istream& operator>> (short& val);
basic_istream& operator>> (unsigned short& val);
basic_istream& operator>> (int& val);
basic_istream& operator>> (unsigned int& val);
basic_istream& operator>> (long& val);
basic_istream& operator>> (unsigned long& val);
basic_istream& operator>> (float& val);
basic_istream& operator>> (double& val);
basic_istream& operator>> (long double& val);
basic_istream& operator>> (void*& val);
stream buffers (2)	
basic_istream& operator>> (basic_streambuf<char_type,traits_type>* sb );
manipulators (3)	
basic_istream& operator>> (basic_istream& (*pf)(basic_istream&));
basic_istream& operator>> (
  basic_ios<char_type,traits_type>& (*pf)(basic_ios<char_type,traits_type>&));
basic_istream& operator>> (ios_base& (*pf)(ios_base&));

C++11

arithmetic types (1)	
basic_istream& operator>> (bool& val);
basic_istream& operator>> (short& val);
basic_istream& operator>> (unsigned short& val);
basic_istream& operator>> (int& val);
basic_istream& operator>> (unsigned int& val);
basic_istream& operator>> (long& val);
basic_istream& operator>> (unsigned long& val);
basic_istream& operator>> (long long& val);
basic_istream& operator>> (unsigned long long& val);
basic_istream& operator>> (float& val);
basic_istream& operator>> (double& val);
basic_istream& operator>> (long double& val);
basic_istream& operator>> (void*& val);
stream buffers (2)	
basic_istream& operator>> (basic_streambuf<char_type,traits_type>* sb );
manipulators (3)	
basic_istream& operator>> (basic_istream& (*pf)(basic_istream&));
basic_istream& operator>> (
  basic_ios<char_type,traits_type>& (*pf)(basic_ios<char_type,traits_type>&));
basic_istream& operator>> (ios_base& (*pf)(ios_base&));

参数

  • val - 存储提取的字符表示的值的对象。请注意,此参数的类型(以及流的格式标志)会影响有效表示的构成。
  • sb - 指向 basic_streambuf 对象的指针,在其受控输出序列上复制字符。

  • pf - 接受并返回流对象的函数。

返回值

返回 basic_istream 对象 (*this)。

例外情况

基本保证- 如果抛出异常,则对象处于有效状态。

数据竞赛

修改 val 或 sb 指向的对象。

例子

在下面的 std::basic_istream::operator 示例中>>

#include <iostream>     

int main () {
   int n;

   std::cout << "Enter a number: ";
   std::cin >> n;
   std::cout << "You have entered: " << n << '\n';

   std::cout << "Enter a hexadecimal number: ";
   std::cin >> std::hex >> n;         
   std::cout << "Its decimal equivalent is: " << n << '\n';

   return 0;
}

让我们编译并运行上面的程序,这将产生以下结果 -

Enter a number: 11
You have entered: 11
Enter a hexadecimal number: 5E
Its decimal equivalent is: 94
istream.htm