Nota
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare ad accedere o modificare le directory.
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare a modificare le directory.
Il modello di classe ostream_iterator descrive un oggetto iteratore di output che scrive elementi successivi nel flusso di output con l'estrazione .
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 . Questo argomento è facoltativo e il valore predefinito è .
Traits
Tipo che rappresenta il tipo di carattere per . Questo argomento è facoltativo e il valore predefinito è .*
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 .
Costruttori
| Costruttore | Descrizione |
|---|---|
ostream_iterator |
Costruisce inizializzato e delimitato per scrivere nel flusso di output. |
Typedef
| Nome tipo | Descrizione |
|---|---|
char_type |
Tipo che fornisce il tipo di carattere di . |
ostream_type |
Tipo che fornisce il tipo di flusso di . |
traits_type |
Tipo che fornisce il tipo di tratti di . |
Operatori
| Operatore | Descrizione |
|---|---|
operator* |
Operatore di dereferenziazione usato per implementare l'espressione dell'iteratore di output . |
operator++ |
Operatore di incremento non funzionale che restituisce 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 per la scrittura in un flusso di output. |
Requisiti
Intestazione:
Spazio dei nomi:
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 .
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;
}
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 .
ostream_iterator<Type, CharType, Traits>& operator*();
Valore restituito
Riferimento a .
Osservazioni:
I requisiti per un iteratore di output che deve soddisfare richiedono che solo l'espressione sia valida e non indichi nulla su o su di essi. L'operatore membro in questa implementazione restituisce .
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;
}
Elements written to output stream:
10
20
30
ostream_iterator::operator++
Operatore di incremento non funzionale che restituisce 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 .
Osservazioni:
Questi operatori membro restituiscono entrambi .
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;
}
Elements written to output stream:
10
20
30
ostream_iterator::operator=
Operatore di assegnazione usato per implementare l'espressione output_iterator per la scrittura in un flusso di output.
ostream_iterator<Type, CharType, Traits>& operator=(const Type& val);
Parametri
val
Valore dell'oggetto di tipo da inserire nel flusso di output.
Valore restituito
L'operatore inserisce nel flusso di output associato all'oggetto , seguito dal delimitatore specificato in (se presente) e quindi restituisce un riferimento all'oggetto .
Osservazioni:
I requisiti per un iteratore di output che deve soddisfare richiedono che solo l'espressione sia valida e non indichi nulla sull'operatore o sull'operatore = autonomamente. Questo operatore membro restituisce .
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;
}
Elements written to output stream:
10
20
30
ostream_iterator::ostream_iterator
Costruisce 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 su cui eseguire l'iterazione.
_Delimiter
Delimitatore inserito nel flusso di output tra i valori.
Osservazioni:
Il primo costruttore inizializza il puntatore del flusso di output con . Il puntatore di stringa del delimitatore designa una stringa vuota.
Il secondo costruttore inizializza il puntatore del flusso di output con e il puntatore di stringa del delimitatore con .
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;
}
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 , una classe di flusso della gerarchia iostream che definisce gli oggetti che possono essere usati per la scrittura.
Esempio
Vedere per un esempio di come dichiarare e usare .
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 .
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;
}
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++