Condividi tramite


Classe istreambuf_iterator

Il modello di classe istreambuf_iterator descrive un oggetto iteratore di input che estrae elementi carattere da un buffer del flusso di input, a cui accede tramite un oggetto archiviato, di puntatore di tipo a basic_streambuf<CharType, Traits.>

Sintassi

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

Parametri

Chartype
Tipo che rappresenta il tipo di carattere per istreambuf_iterator.

Tratti
Tipo che rappresenta il tipo di carattere per istreambuf_iterator. Questo argomento è facoltativo e il valore predefinito è char_traits<CharType.>

Osservazioni:

La classe istreambuf_iterator deve soddisfare i requisiti per un iteratore di input.

Dopo la costruzione o l'incremento di un oggetto della classe istreambuf_iterator con un puntatore archiviato diverso da Null, l'oggetto prova a estrarre e ad archiviare un oggetto di tipo CharType dal flusso di input associato. L'estrazione può tuttavia essere ritardata fino a quando l'oggetto non viene dereferenziato o copiato. Se l'estrazione ha esito negativo, l'oggetto sostituisce il puntatore archiviato con un puntatore Null, creando così un indicatore di fine della sequenza.

Costruttori

Costruttore Descrizione
istreambuf_iterator Costruisce un istreambuf_iterator inizializzato per leggere i caratteri dal flusso di input.

Typedef

Nome tipo Descrizione
char_type Tipo che fornisce il tipo di carattere di ostreambuf_iterator.
int_type Tipo che fornisce un tipo Integer per istreambuf_iterator.
istream_type Tipo che fornisce il tipo di flusso di istream_iterator.
streambuf_type Tipo che fornisce il tipo di flusso di istreambuf_iterator.
traits_type Tipo che fornisce il tipo di tratti di istream_iterator.

Funzioni membro

Funzione membro Descrizione
equal Test per verificare l'uguaglianza tra due iteratori del buffer del flusso di input.

Operatori

Operatore Descrizione
operator* L'operatore di dereferenziazione restituisce il carattere successivo del flusso.
operator++ Restituisce il carattere successivo del flusso di input oppure copia l'oggetto prima di incrementarlo e restituisce la copia.

Requisiti

Header:<iterator>

Spazio dei nomi: std

istreambuf_iterator::char_type

Tipo che fornisce il tipo di carattere di ostreambuf_iterator.

typedef CharType char_type;

Osservazioni:

Il tipo è un sinonimo del parametro di modello CharType.

Esempio

// 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

Test per verificare l'equivalenza tra due iteratori del buffer del flusso di input.

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

Parametri

right
Iteratore per cui verificare l'equivalenza.

Valore restituito

true se entrambi istreambuf_iteratorsono iteratori end-of-stream o se nessuno dei due è un iteratore end-of-stream; in caso contrario false, .

Osservazioni:

Un intervallo è definito dalla istreambuf_iterator posizione corrente e dall'iteratore di fine flusso, ma poiché tutti gli iteratori di flusso non end-of-end sono equivalenti sotto la equal funzione membro, non è possibile definire eventuali intervalli secondari usando istreambuf_iterators. Gli operatori == e != hanno la stessa semantica.

Esempio

// 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

Tipo che fornisce un tipo Integer per istreambuf_iterator.

typedef typename traits_type::int_type int_type;

Osservazioni:

Il tipo è sinonimo di Traits::int_type.

Esempio

// 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

Tipo che fornisce il tipo di flusso di istreambuf_iterator.

typedef basic_istream<CharType, Traits> istream_type;

Osservazioni:

Il tipo è un sinonimo di basic_istream<CharType, Traits.>

Esempio

Vedere istreambuf_iterator per un esempio di come dichiarare e usare istream_type.

istreambuf_iterator::istreambuf_iterator

Costruisce un oggetto istreambuf_iterator inizializzato per la lettura di caratteri dal flusso di input.

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

Parametri

strbuf
Buffer del flusso di input a cui viene collegato l'oggetto istreambuf_iterator.

_Istr
Flusso di input a cui viene collegato l'oggetto istreambuf_iterator.

Osservazioni:

Il primo costruttore inizializza il puntatore del buffer del flusso di input con strbuf. Il secondo costruttore inizializza il puntatore del buffer del flusso di input con _Istr. rdbuf, quindi tenta di estrarre e archiviare un oggetto di tipo CharType.

Esempio

// 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*

L'operatore di dereferenziazione restituisce il carattere successivo del flusso.

CharType operator*() const;

Valore restituito

Carattere successivo nel flusso.

Esempio

// 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++

Restituisce il carattere successivo del flusso di input oppure copia l'oggetto prima di incrementarlo e restituisce la copia.

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

Valore restituito

Oggetto istreambuf_iterator o riferimento a un oggetto istreambuf_iterator.

Osservazioni:

Il primo operatore tenta infine di estrarre e archiviare un oggetto di tipo CharType dal flusso di input associato. Il secondo operatore esegue una copia dell'oggetto, incrementa l'oggetto, quindi restituisce la copia.

Esempio

// 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

Tipo che fornisce il tipo di flusso dell'oggetto istreambuf_iterator.

typedef basic_streambuf<CharType, Traits> streambuf_type;

Osservazioni:

Il tipo è un sinonimo di basic_streambuf<CharType, Traits.>

Esempio

Vedere istreambuf_iterator per un esempio di come dichiarare e usare istreambuf_type.

istreambuf_iterator::traits_type

Tipo che fornisce il tipo di tratti di istream_iterator.

typedef Traits traits_type;

Osservazioni:

Il tipo è un sinonimo del parametro di modello Traits.

Esempio

// 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 , ' ' , '.' );
}

Vedi anche

Struct iterator
<iterator>
Thread Safety in the C++ Standard Library (Sicurezza dei thread nella libreria standard C++)
Informazioni di riferimento per la libreria standard C++