ostream_iterator – třída
Šablona třídy ostream_iterator popisuje výstupní objekt iterátoru, který zapisuje následné prvky do výstupního datového proudu s extrakcí operator <<
.
Syntaxe
template <class Type class CharType = char class Traits = char_traits <CharType>>
class ostream_iterator
Parametry
Typ
Typ objektu, který má být vložen do výstupního toku.
CharType
Typ, který představuje typ znaku ostream_iterator
pro znak . Tento argument je nepovinný a výchozí hodnota je char
.
Rysy
Typ, který představuje typ znaku ostream_iterator
pro znak . Tento argument je nepovinný a výchozí hodnota je char_traits
<CharType.>
Třída ostream_iterator musí splňovat požadavky na výstupní iterátor. Algoritmy lze zapisovat přímo do výstupních datových proudů pomocí .ostream_iterator
Konstruktory
Konstruktor | Popis |
---|---|
ostream_iterator | ostream_iterator Vytvoří inicializovanou a oddělenou pro zápis do výstupního datového proudu. |
Typedefs
Název typu | Popis |
---|---|
char_type | Typ, který poskytuje typ znaku ostream_iterator . |
ostream_type | Typ, který poskytuje typ datového ostream_iterator proudu . |
traits_type | Typ, který poskytuje typ znakových ostream_iterator vlastností . |
Operátory
Operátor | Popis |
---|---|
operátor* | Operátor dereferencing použitý k implementaci výstupního výrazu iterátoru * i = x . |
operator++ | Nefunkční inkrementační operátor, který vrací stejný ostream_iterator objekt, který byl vyřešen před zavolání operace. |
operator= | Operátor přiřazení použitý k implementaci výrazu výstupního iterátoru * i = x pro zápis do výstupního datového proudu. |
Požadavky
Header:<iterator>
Obor názvů: std
ostream_iterator::char_type
Typ, který poskytuje typ znaku iterátoru.
typedef CharType char_type;
Poznámky
Typ je synonymem pro parametr CharType
šablony .
Příklad
// 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*
Operátor dereferencing použitý k implementaci výstupního výrazu iterátoru * ii = x.
ostream_iterator<Type, CharType, Traits>& operator*();
Návratová hodnota
Odkaz na ostream_iterator
.
Poznámky
Požadavky pro výstupní iterátor, který ostream_iterator
musí splňovat, vyžadují pouze výraz * ii = t být platný a o sobě nic nehlásí.operator
operator=
Operátor člena v této implementaci vrátí *this
.
Příklad
// 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++
Nefunkční inkrementační operátor, který vrací stejný ostream_iterator
objekt, který byl vyřešen před zavolání operace.
ostream_iterator<Type, CharType, Traits>& operator++();
ostream_iterator<Type, CharType, Traits> operator++(int);
Návratová hodnota
Odkaz na ostream_iterator
.
Poznámky
Tyto členské operátory vrátí oba operátory *this
.
Příklad
// 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=
Operátor přiřazení použitý k implementaci výrazu output_iterator * i
= x
pro zápis do výstupního datového proudu.
ostream_iterator<Type, CharType, Traits>& operator=(const Type& val);
Parametry
Val
Hodnota objektu typu Type
, který se má vložit do výstupního datového proudu.
Návratová hodnota
Operátor vloží hodnotu do výstupního datového proudu přidruženého k objektu, následovaný oddělovačem zadaným v konstruktoru ostream_iterator (pokud existuje) a poté vrátí odkaz na ostream_iterator
.
Poznámky
Požadavky pro výstupní iterátor, který ostream_iterator
musí splňovat, vyžadují pouze výraz * ii
= t
platný a nehlásí nic o operátoru nebo operátoru = samostatně. Tento operátor členu vrátí *this
.
Příklad
// 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
ostream_iterator
Vytvoří inicializovanou a oddělenou pro zápis do výstupního datového proudu.
ostream_iterator(
ostream_type& _Ostr);
ostream_iterator(
ostream_type& _Ostr,
const CharType* _Delimiter);
Parametry
_Ostr
Výstupní datový proud typu ostream_iterator::ostream_type , který má být iterated.
_Oddělovač
Oddělovač vložený do výstupního datového proudu mezi hodnotami.
Poznámky
První konstruktor inicializuje ukazatel výstupního datového proudu s &_Ostr
. Ukazatel řetězce oddělovače označuje prázdný řetězec.
Druhý konstruktor inicializuje ukazatel výstupního datového proudu a &_Ostr
ukazatel řetězce oddělovače s _Delimiter.
Příklad
// 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
Typ, který poskytuje typ datového proudu iterátoru.
typedef basic_ostream<CharType, Traits> ostream_type;
Poznámky
Typ je synonymem pro basic_ostreamCharType
<, Traits
>třída streamu iostream hierarchie, která definuje objekty, které lze použít k zápisu.
Příklad
Příklad deklarace a použití ostream_type
najdete v ostream_iterator .
ostream_iterator::traits_type
Typ, který poskytuje typ znakových vlastností iterátoru.
typedef Traits traits_type;
Poznámky
Typ je synonymem pro parametr Traits
šablony .
Příklad
// 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
*/
Viz také
<iterátor>
Bezpečný přístup z více vláken ve standardní knihovně C++
Standardní knihovna C++ – referenční dokumentace