C++ basic_istream 库 - operator>>
描述
应用于输入流的这种运算符 (>>) 称为提取运算符。
声明
以下是 std::basic_istream::operator>> 的声明。
C++98
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
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 指向的对象并修改流对象。
异常与安全
Basic guarantee − 如果抛出异常,则对象处于有效状态。
示例
在下面的 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: 1 You have entered: 1 Enter a hexadecimal number: 5E Its decimal equivalent is: 94