Classe ostream_iterator

Il modello di classe ostream_iterator descrive un oggetto iteratore di output che scrive elementi successivi nel flusso di output con l'estrazione operator <<.

Sintassi

template <class Type class CharType = char class Traits = char_traits <CharType>>
class ostream_iterator

Parametri

Type
Il tipo di oggetto da inserire nel flusso di output.

Chartype
Tipo che rappresenta il tipo di carattere per ostream_iterator. Questo argomento è facoltativo e il valore predefinito è char.

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

La classe ostream_iterator deve soddisfare i requisiti per un iteratore di output. Gli algoritmi possono essere scritti direttamente nei flussi di output utilizzando un ostream_iterator.

Costruttori

Costruttore Descrizione
ostream_iterator Costruisce ostream_iterator inizializzato e delimitato per scrivere nel flusso di output.

Typedef

Nome tipo Descrizione
char_type Tipo che fornisce il tipo di carattere di ostream_iterator.
ostream_type Tipo che fornisce il tipo di flusso di ostream_iterator.
traits_type Tipo che fornisce il tipo di tratti di ostream_iterator.

Operatori

Operatore Descrizione
operator* Operatore di dereferenziazione usato per implementare l'espressione dell'iteratore di output * i = x.
operator++ Operatore di incremento non funzionale che restituisce ostream_iterator allo stesso oggetto a cui ha puntato prima che venisse chiamata l'operazione.
operator= Operatore di assegnazione usato per implementare l'espressione dell'iteratore di output * i = x per la scrittura in un flusso di output.

Requisiti

Header:<iterator>

Spazio dei nomi: std

ostream_iterator::char_type

Tipo che fornisce il tipo di carattere dell'iteratore.

typedef CharType char_type;

Osservazioni:

Il tipo è un sinonimo del parametro di modello CharType.

Esempio

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

int main( )
{
   using namespace std;

   typedef ostream_iterator<int>::char_type CHT1;
   typedef ostream_iterator<int>::traits_type CHTR1;

   // ostream_iterator for stream cout
   // with new line delimiter:
    ostream_iterator<int, CHT1, CHTR1> intOut ( cout , "\n" );

   // Standard iterator interface for writing
   // elements to the output stream:
   cout << "The integers written to the output stream\n"
        << "by intOut are:" << endl;
*intOut = 10;
*intOut = 20;
*intOut = 30;
}
/* Output:
The integers written to the output stream
by intOut are:
10
20
30
*/

ostream_iterator::operator*

Operatore di dereferenziazione usato per implementare l'espressione dell'iteratore di output * ii = x.

ostream_iterator<Type, CharType, Traits>& operator*();

Valore restituito

Riferimento a ostream_iterator.

Osservazioni:

I requisiti per un iteratore di output che ostream_iterator deve soddisfare richiedono solo l'espressione * ii = t essere validi e non dice nulla su operator o operator= su di essi. L'operatore membro in questa implementazione restituisce *this.

Esempio

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

int main( )
{
   using namespace std;

   // ostream_iterator for stream cout
   // with new line delimiter
   ostream_iterator<int> intOut ( cout , "\n" );

   // Standard iterator interface for writing
   // elements to the output stream
   cout << "Elements written to output stream:" << endl;
*intOut = 10;
   intOut++;      // No effect on iterator position
*intOut = 20;
*intOut = 30;
}
/* Output:
Elements written to output stream:
10
20
30
*/

ostream_iterator::operator++

Operatore di incremento non funzionale che restituisce ostream_iterator allo stesso oggetto a cui ha puntato prima che venisse chiamata l'operazione.

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

Valore restituito

Riferimento a ostream_iterator.

Osservazioni:

Questi operatori membro restituiscono *thisentrambi .

Esempio

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

int main( )
{
   using namespace std;

   // ostream_iterator for stream cout
   // with new line delimiter
   ostream_iterator<int> intOut ( cout , "\n" );

   // standard iterator interface for writing
   // elements to the output stream
   cout << "Elements written to output stream:" << endl;
*intOut = 10;
   intOut++;      // No effect on iterator position
*intOut = 20;
*intOut = 30;
}
/* Output:
Elements written to output stream:
10
20
30
*/

ostream_iterator::operator=

Operatore di assegnazione usato per implementare l'espressione di output_iterator * i = x per la scrittura in un flusso di output.

ostream_iterator<Type, CharType, Traits>& operator=(const Type& val);

Parametri

val
Valore dell'oggetto di tipo Type da inserire nel flusso di output.

Valore restituito

L'operatore inserisce val nel flusso di output associato all'oggetto , seguito dal delimitatore specificato nel costruttore ostream_iterator (se presente) e quindi restituisce un riferimento a ostream_iterator.

Osservazioni:

I requisiti per un iteratore di output che devono essere soddisfatti da ostream_iterator richiedono solo che l'espressione * ii = t sia valida e non riguardano individualmente gli operatori operator o operator=. Questo operatore membro restituisce *this.

Esempio

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

int main( )
{
   using namespace std;

   // ostream_iterator for stream cout
   // with new line delimiter
   ostream_iterator<int> intOut ( cout , "\n" );

   // Standard iterator interface for writing
   // elements to the output stream
   cout << "Elements written to output stream:" << endl;
*intOut = 10;
   intOut++;      // No effect on iterator position
*intOut = 20;
*intOut = 30;
}
/* Output:
Elements written to output stream:
10
20
30
*/

ostream_iterator::ostream_iterator

Costruisce ostream_iterator inizializzato e delimitato per scrivere nel flusso di output.

ostream_iterator(
    ostream_type& _Ostr);

ostream_iterator(
    ostream_type& _Ostr,
    const CharType* _Delimiter);

Parametri

_Ostr
Flusso di output di tipo ostream_iterator:: ostream_type di cui eseguire l'iterazione.

_Delimitatore
Delimitatore inserito nel flusso di output tra i valori.

Osservazioni:

Il primo costruttore inizializza il puntatore del flusso di output con &_Ostr. Il puntatore di stringa del delimitatore designa una stringa vuota.

Il secondo costruttore inizializza il puntatore del flusso di output con &_Ostr e il puntatore di stringa del delimitatore con _Delimiter.

Esempio

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

int main( )
{
   using namespace std;

   // ostream_iterator for stream cout
   ostream_iterator<int> intOut ( cout , "\n" );
*intOut = 10;
   intOut++;
*intOut = 20;
   intOut++;

   int i;
   vector<int> vec;
   for ( i = 1 ; i < 7 ; ++i )
   {
      vec.push_back (  i );
   }

   // Write elements to standard output stream
   cout << "Elements output without delimiter: ";
   copy ( vec.begin ( ), vec.end ( ),
          ostream_iterator<int> ( cout ) );
   cout << endl;

   // Write elements with delimiter " : " to output stream
   cout << "Elements output with delimiter: ";
   copy ( vec.begin ( ), vec.end ( ),
          ostream_iterator<int> ( cout, " : " ) );
   cout << endl;
}
/* Output:
10
20
Elements output without delimiter: 123456
Elements output with delimiter: 1 : 2 : 3 : 4 : 5 : 6 :
*/

ostream_iterator::ostream_type

Tipo che fornisce il tipo di flusso dell'iteratore.

typedef basic_ostream<CharType, Traits> ostream_type;

Osservazioni:

Il tipo è un sinonimo di basic_ostreamCharType<, Traits>, una classe di flusso della gerarchia iostream che definisce gli oggetti che possono essere usati per la scrittura.

Esempio

Vedere ostream_iterator per indicazioni su come dichiarare e usare ostream_type.

ostream_iterator::traits_type

Tipo che fornisce il tipo di tratti di carattere dell'iteratore.

typedef Traits traits_type;

Osservazioni:

Il tipo è un sinonimo del parametro di modello Traits.

Esempio

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

int main( )
{
   using namespace std;

   // The following not OK, but are just the default values:
   typedef ostream_iterator<int>::char_type CHT1;
   typedef ostream_iterator<int>::traits_type CHTR1;

   // ostream_iterator for stream cout
   // with new line delimiter:
    ostream_iterator<int, CHT1, CHTR1> intOut ( cout , "\n" );

   // Standard iterator interface for writing
   // elements to the output stream:
   cout << "The integers written to output stream\n"
        << "by intOut are:" << endl;
*intOut = 1;
*intOut = 10;
*intOut = 100;
}
/* Output:
The integers written to output stream
by intOut are:
1
10
100
*/

Vedi anche

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