共用方式為


istreambuf_iterator 類別

類別範本istreambuf_iterator描述輸入反覆運算器物件,該物件會從輸入資料流程緩衝區擷取字元元素,而該緩衝區會透過儲存的物件來存取該物件、CharType Traits > 的類型指標 basic_streambuf<

語法

template <class CharType class Traits = char_traits <CharType>>
class istreambuf_iterator
: public iterator<input_iterator_tag, CharType, typename Traits ::off_type, CharType*, CharType&>

參數

CharType
類型,表示 istreambuf_iterator 的字元類型。

特性
類型,表示 istreambuf_iterator 的字元類型。 這個引數是選擇性的,預設值為 char_traits< CharType > 。

備註

istreambuf_iterator 類別必須符合輸入迭代器的需求。

在建構或遞增具有非 Null 預存指標的 istreambuf_iterator 類別物件之後,物件會實際嘗試從關聯的輸入資料流中擷取和儲存 CharType 類型物件。 不過,擷取可能會延遲,直到物件被取值或複製為止。 如果擷取失敗,物件是實際上會將儲存的指標取代為 null 指標,因而建立序列結尾指標。

建構函式

建構函式 描述
istreambuf_iterator 建構 istreambuf_iterator,初始化以從輸入資料流讀取字元。

Typedefs

類型名稱 描述
char_type 類型,提供 ostreambuf_iterator 的字元類型。
int_type 類型,提供 istreambuf_iterator 的整數類型。
istream_type 類型,提供 istream_iterator 的資料流類型。
streambuf_type 類型,提供 istreambuf_iterator 的資料流類型。
traits_type 類型,提供 istream_iterator 的字元特性類型。

成員函式

成員函數 描述
equal 測試兩個輸入資料流緩衝區迭代器是否相等。

操作員

運算子 描述
operator* 取值運算子傳回資料流的下一個字元。
operator++ 從輸入資料流傳回下一個字元,或在遞增之前複製物件並傳回複本。

需求

Header: < iterator>

命名空間:std

istreambuf_iterator::char_type

類型,提供 ostreambuf_iterator 的字元類型。

typedef CharType char_type;

備註

此類型與範本參數 CharType 同義。

範例

// istreambuf_iterator_char_type.cpp
// compile with: /EHsc
#include <iterator>
#include <vector>
#include <iostream>
#include <algorithm>

int main( )
{
   using namespace std;

   typedef istreambuf_iterator<char>::char_type CHT1;
   typedef istreambuf_iterator<char>::traits_type CHTR1;

   cout << "(Try the example: 'So many dots to be done'\n"
        << " then an Enter key to insert into the output,\n"
        << " & use a ctrl-Z Enter key combination to exit): ";

   // istreambuf_iterator for input stream
   istreambuf_iterator< CHT1, CHTR1> charInBuf ( cin );
   ostreambuf_iterator<char> charOut ( cout );

   // Used in conjunction with replace_copy algorithm
   // to insert into output stream and replace spaces
   // with dot-separators
   replace_copy ( charInBuf , istreambuf_iterator<char>( ),
        charOut , ' ' , '.' );
}

istreambuf_iterator::equal

測試兩個輸入資料流緩衝區迭代器是否相等。

bool equal(const istreambuf_iterator<CharType, Traits>& right) const;

參數

right
要檢查其相等性的迭代器。

傳回值

true 如果兩者 istreambuf_iterator 都是資料流程結尾反覆運算器,或者兩者都不是資料流程結尾反覆運算器,則為 ,否則 false 為 。

備註

範圍是由 istreambuf_iterator 目前位置和資料流程結尾反覆運算器所定義,但由於所有非資料流程反覆運算器在成員函式下 equal 都相等,因此無法使用 來定義任何子範圍 istreambuf_iterator==!= 運算子的語意相同。

範例

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

int main( )
{
   using namespace std;

   cout << "(Try the example: 'Hello world!'\n"
        << " then an Enter key to insert into the output,\n"
        << " & use a ctrl-Z Enter key combination to exit): ";

   istreambuf_iterator<char> charReadIn1 ( cin );
   istreambuf_iterator<char> charReadIn2 ( cin );

   bool b1 = charReadIn1.equal ( charReadIn2 );

   if (b1)
      cout << "The iterators are equal." << endl;
   else
      cout << "The iterators are not equal." << endl;
}

istreambuf_iterator::int_type

類型,提供 istreambuf_iterator 的整數類型。

typedef typename traits_type::int_type int_type;

備註

此類型是 Traits::int_type的同義字。

範例

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

int main( )
{
   using namespace std;
   istreambuf_iterator<char>::int_type inttype1 = 100;
   cout << "The inttype1 = " << inttype1 << "." << endl;
}
/* Output:
The inttype1 = 100.
*/

istreambuf_iterator::istream_type

類型,提供 istreambuf_iterator 的資料流類型。

typedef basic_istream<CharType, Traits> istream_type;

備註

此類型與 CharType Traits > 同義 basic_istream<

範例

如需如何宣告及使用 istream_type 的範例,請參閱 istreambuf_iterator

istreambuf_iterator::istreambuf_iterator

建構會初始化以從輸入資料流讀取字元的 istreambuf_iterator。

istreambuf_iterator(streambuf_type* strbuf = 0) throw();
istreambuf_iterator(istream_type& _Istr) throw();

參數

strbuf
要附加 istreambuf_iterator 的輸入資料流緩衝區。

_Istr
要附加 istreambuf_iterator 的輸入資料流。

備註

第一個建構函式會使用 strbuf 初始化輸入資料流程緩衝區指標。 第二個建構函式會使用 _Istr 初始化輸入資料流程緩衝區指標。 rdbuf,最後會嘗試擷取和儲存 類型 CharType 的物件。

範例

// istreambuf_iterator_istreambuf_iterator.cpp
// compile with: /EHsc
#include <iterator>
#include <vector>
#include <algorithm>
#include <iostream>

int main( )
{
   using namespace std;

   // Following declarations will not compile:
   istreambuf_iterator<char>::istream_type &istrm = cin;
   istreambuf_iterator<char>::streambuf_type *strmbf = cin.rdbuf( );

   cout << "(Try the example: 'Oh what a world!'\n"
      << " then an Enter key to insert into the output,\n"
      << " & use a ctrl-Z Enter key combination to exit): ";
   istreambuf_iterator<char> charReadIn ( cin );
   ostreambuf_iterator<char> charOut ( cout );

   // Used in conjunction with replace_copy algorithm
   // to insert into output stream and replace spaces
   // with hyphen-separators
   replace_copy ( charReadIn , istreambuf_iterator<char>( ),
      charOut , ' ' , '-' );
}

istreambuf_iterator::operator*

取值運算子傳回資料流的下一個字元。

CharType operator*() const;

傳回值

資料流中的下一個字元。

範例

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

int main( )
{
   using namespace std;

   cout << "Type string of characters & enter to output it,\n"
      << " with stream buffer iterators,(try: 'I'll be back.')\n"
      << " repeat as many times as desired,\n"
      << " then keystroke ctrl-Z Enter to exit program: ";
   istreambuf_iterator<char> inpos ( cin );
   istreambuf_iterator<char> endpos;
   ostreambuf_iterator<char> outpos ( cout );
   while ( inpos != endpos )
   {
*outpos = *inpos;   //Put value of outpos equal to inpos
      ++inpos;
      ++outpos;
   }
}

istreambuf_iterator::operator++

從輸入資料流傳回下一個字元,或在遞增之前複製物件並傳回複本。

istreambuf_iterator<CharType, Traits>& operator++();
istreambuf_iterator<CharType, Traits> operator++(int);

傳回值

istreambuf_iterator 或對 istreambuf_iterator 的參考。

備註

第一個運算子最終會嘗試從相關聯的輸入資料流程擷取和儲存型 CharType 別的物件。 第二個運算子會複製物件、遞增物件,然後傳回複本。

範例

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

int main( )
{
   using namespace std;

   cout << "Type string of characters & enter to output it,\n"
      << " with stream buffer iterators,(try: 'I'll be back.')\n"
      << " repeat as many times as desired,\n"
      << " then keystroke ctrl-Z Enter to exit program: ";
   istreambuf_iterator<char> inpos ( cin );
   istreambuf_iterator<char> endpos;
   ostreambuf_iterator<char> outpos ( cout );
   while ( inpos != endpos )
   {
*outpos = *inpos;
      ++inpos;   //Increment istreambuf_iterator
      ++outpos;
   }
}

istreambuf_iterator::streambuf_type

一種類型,提供 istreambuf_iterator 的資料流類型。

typedef basic_streambuf<CharType, Traits> streambuf_type;

備註

此類型與 CharType Traits > 同義 basic_streambuf<

範例

如需如何宣告及使用 istreambuf_type 的範例,請參閱 istreambuf_iterator

istreambuf_iterator::traits_type

類型,提供 istream_iterator 的字元特性類型。

typedef Traits traits_type;

備註

此類型與範本參數 Traits 同義。

範例

// istreambuf_iterator_traits_type.cpp
// compile with: /EHsc
#include <iterator>
#include <vector>
#include <iostream>
#include <algorithm>

int main( )
{
   using namespace std;

   typedef istreambuf_iterator<char>::char_type CHT1;
   typedef istreambuf_iterator<char>::traits_type CHTR1;

   cout << "(Try the example: 'So many dots to be done'\n"
        << " then an Enter key to insert into the output,\n"
        << " & use a ctrl-Z Enter key combination to exit): ";

   // istreambuf_iterator for input stream
   istreambuf_iterator< CHT1, CHTR1> charInBuf ( cin );
   ostreambuf_iterator<char> charOut ( cout );

   // Used in conjunction with replace_copy algorithm
   // to insert into output stream and replace spaces
   // with dot-separators
   replace_copy ( charInBuf , istreambuf_iterator<char>( ),
        charOut , ' ' , '.' );
}

另請參閱

iterator 結構
<iterator>
C++ 標準程式庫中的執行緒安全
C++ 標準程式庫參考