共用方式為


basic_istream::operator>>

會在輸入資料流的函式或讀取輸入資料流的格式化資料。

basic_istream& operator>>(
   basic_istream& (*_Pfn)(basic_istream&)
);
basic_istream& operator>>(
   ios_base& (*_Pfn)(ios_base&)
);
basic_istream& operator>>(
   basic_ios<Elem, Tr>& (*_Pfn)(basic_ios<Elem, Tr>&))
;
basic_istream& operator>>(
   basic_streambuf<Elem, Tr> *_Strbuf
);
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>>(
   void *& _Val
);
basic_istream& operator>>(
   float& _Val
);
basic_istream& operator>>(
   double& _Val
);
basic_istream& operator>>(
   long double& _Val
);

參數

  • _Pfn
    函式指標。

  • _Strbuf
    型別 stream_buf物件。

  • _Val
    讀取的值從資料流。

傳回值

資料流 (*this)。

備註

<istream> 標題也會定義幾個擷取全域運算子。 如需詳細資訊,請參閱 operator>> (<istream>)

第 10% 成員函式可確保表單 istr >> ws 的運算式呼叫 ws(istr),然後傳回 *this。 第二個和第三個函式可確保其他操作工具,例如 hex,類似的行為。 其餘的函式複合格式的輸入函式。

函式:

basic_istream& operator>>(
    basic_streambuf<Elem, Tr> *_Strbuf);

在 _Strbuf擷取項目,則為,如果 _Strbuf 不為 null 指標,並插入資料錄。 在檔案結尾的擷取停止。 它也會停止,而不會擷取該項目,則為,如果插入失敗或擲回例外狀況,但會攔截的例外狀況 (不會重新擲回)。 如果函式未擷取項目,它會呼叫 setstate(failbit)。 在任何情況下,函式會傳回 *this

函式:

basic_istream& operator>>(bool& _Val);

擷取資料行並將它轉換成布林值呼叫 use_facet <num_get<ElemInIt> (getloc)。 取得(InIt( rdbuf), Init(0), *thisgetloc, _Val)。 在此例中, InIt 定義為 istreambuf_iterator<ElemTr>。 函式會傳回 *this

函式:

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>>(void *& _Val);

每一個擷取資料行並將它轉換成數值藉由呼叫 use_facet<num_get<ElemInIt> (getloc)。 取得(InIt( rdbuf), Init(0), *thisgetloc, _Val)。 在這裡,,因為 istreambuf_iterator<ElemTr>和 _Val 具有型別 long*、*unsigned long或 void * 如有需要, InIt 定義。

如果已轉換的值不能表示為 _Val類型,函式會 setstate(failbit)。 在任何情況下,函式會傳回 *this

函式:

basic_istream& operator>>(float& _Val);
basic_istream& operator>>(double& _Val);
basic_istream& operator>>(long double& _Val);

每一個擷取資料行並將它轉換成數值藉由呼叫 use_facet<num_get<ElemInIt> (getloc)。 get(InIt( rdbuf), Init(0), *thisgetloc, _Val)。 在這裡,,因為 istreambuf_iterator<ElemTr>和 _Val 具有型別 double 或 long double 如有需要, InIt 定義。

如果已轉換的值不能表示為 _Val類型,函式會 setstate(failbit)。 不論是哪種情況,則會傳回 *this

範例

// istream_basic_istream_op_is.cpp
// compile with: /EHsc
#include <iostream>

using namespace std;

ios_base& hex2( ios_base& ib ) 
{
   ib.unsetf( ios_base::dec );
   ib.setf( ios_base::hex );
   return ib;
}

basic_istream<char, char_traits<char> >& somefunc(basic_istream<char, char_traits<char> > &i)
{
   if ( i == cin ) 
   {
      cerr << "i is cin" << endl;
   }
   return i;
}

int main( ) 
{
   int i = 0;
   cin >> somefunc;
   cin >> i;
   cout << i << endl;
   cin >> hex2;
   cin >> i;
   cout << i << endl;
}

輸入

10
10

範例輸出

i is cin
10
10
10
16

需求

標題: <istream>

命名空間: std

請參閱

參考

basic_istream Class

operator>> (<istream>)

iostream 程式設計

iostreams 慣例