The program examples for the member used functions compiled usingVC++7.0 / .Net, win32 empty console mode application. This is a continuation from the previous Module.Linuxg++ compilation examples given at the end of this Module. The source code for this tutorial is available inC++ STL Iterator source code samples.
| The istream_iterator::traits_type
typedef Traits traits_type;
Member Functions
The istreambuf_iterator::equal
bool equal(const istreambuf_iterator& _Right) const;
Parameter
|
// istreambuf_iterator, equal
#include <iterator>
#include <iostream>
using namespace std;
int main()
{
cout<<"\nOperation: bol = readchinpt1.equal(readchinpt2)\n";
cout<<"Enter a line of text then an Enter key to\ninsert into the output:\n";
istreambuf_iterator<char> readchinpt1(cin);
istreambuf_iterator<char> readchinpt2(cin);
bool bol = readchinpt1.equal(readchinpt2);
if(bol)
cout<<"The iterators are equal."<<endl;
else
cout<<"The iterators are not equal."<<endl;
return 0;
}
Constructs an istreambuf_iterator that is initialized to read characters from the input stream.
istreambuf_iterator ( streambuf_type* _Strbuf = 0 ) throw();
istreambuf_iterator ( istream_type& _Istr ) throw();
Parameter | Description |
_Strbuf | The input stream buffer to which the istreambuf_iterator is being attached. |
_Istr | The input stream to which theistreambuf_iterator is being attached. |
Table 32.11 |
The first constructor initializes the input stream-buffer pointer with_Strbuf.
The second constructor initializes the input stream-buffer pointer with_Istr.rdbuf, and then eventually attempts to extract and store an object of type CharType.
// istreambuf_iterator, istreambuf_iterator
#include <iterator>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
istreambuf_iterator<char>::istream_type &istrm = cin;
istreambuf_iterator<char>::streambuf_type *strmbf = cin.rdbuf();
cout<<"Enter a line of text, then an Enter key to insert into\n"
<<"the output, (& use a ctrl-Z Enter key combination to exit):\n";
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, ' ', '-');
return 0;
}
Operator | Description |
operator* | The dereferencing operator returns the next character in the stream. |
operator++ | Either returns the next character from the input stream or copies the object before incrementing it and returns the copy. |
operator-> | Returns the value of a member, if any. |
Table 32.12 |
Either returns the next character from the input stream or copies the object before incrementing it and returns the copy.
istreambuf_iterator& operator++();
istreambuf_iterator operator++(int);
The return value is an istreambut_iterator or a reference to an istreambuf_iterator.
The first operator eventually attempts to extract and store an object of typeCharType from the associated input stream.
The second operator makes a copy of the object, increments the object, and then returns the copy.
// istreambuf_iterator, operator++
#include <iterator>
#include <iostream>
using namespace std;
int main()
{
cout<<"Type a line of text & enter to output it, with stream\n"
<<"buffer iterators, repeat as many times as desired,\n"
<<"then keystroke ctrl-Z Enter to exit program: \n";
istreambuf_iterator<char> inpos(cin);
istreambuf_iterator<char> endpos;
ostreambuf_iterator<char> outpos(cout);
while(inpos != endpos)
{
*outpos = *inpos;
// increment istreambuf_iterator
++inpos;
++outpos;
}
return 0;
}
The template class ostream_iterator describes an output iterator object that writes successive elements onto the output stream with the extraction operator >>.
template <
class Type
class CharType = char
class Traits = char_traits<CharType> >
Parameter | Description |
Type | The type of object to be inserted into the output stream. |
CharType | The type that represents the character type for the ostream_iterator. This argument is optional and the default value is char. |
Traits | The type that represents the character type for the ostream_iterator. This argument is optional and the default value is char_traits<CharType>. |
Table 32.13 |
Theostream_iterator class must satisfy the requirements for an output iterator.
Algorithms can be written directly to output streams using an ostream_iterator.
Typedef | Description |
char_type | A type that provides for the character type of the ostream_iterator. |
ostream_type | A type that provides for the stream type of the ostream_iterator. |
traits_type | A type that provides for the character traits type of the ostream_iterator. |
Table 32.14 |
The ostream_iterator::ostream_iterator
Parameters
|
// ostream_iterator, ostream_iterator
#include <iterator>
#include <vector>
#include <iostream>
using namespace std;
int main()
{
// ostream_iterator for stream cout
ostream_iterator<int> intOut(cout, "\n");
*intOut = 12;
intOut++;
*intOut = 33;
intOut++;
int i;
vector<int> vec;
for(i = 10; i<=15; ++i)
vec.push_back(i);
cout<<"Operation: with and without delimiter...\n";
// 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;
return 0;
}
Member function | Description |
ostream_iterator | Constructs an ostream_iterator that is initialized and delimited to write to the output stream. |
Table 32.16 |
Operator | Description |
operator* | Dereferencing operator used to implement the output iterator expression such as *i = x. |
operator++ | A nonfunctional increment operator that returns an ostream_iterator to the same object it addressed before the operation was called. |
operator= | Assignment operator used to implement the output iterator expression such as *i = x for writing to an output stream. |
Table 32.17 |
Assignment operator used to implement the output_iterator expression such as *i = x for writing to an output stream.
ostream_iterator<Type, CharType, Traits>& operator=(const Type& _Val);
Parameter | Description |
_Val | The value of the object of type Type to be inserted into the output stream. |
Table 32.18 |
The return value is the operator inserts _Val into the output stream associated with the object, and then returns a reference to the ostream_iterator.
The requirements for an output iterator that the ostream_iterator must satisfy require only the expression such as *j = x to be valid and says nothing about the operator or theoperator= on their own.
This member operator returns *this pointer.
// ostream_iterator, operator=
#include <iterator>
#include <vector>
#include <iostream>
using namespace std;
int main()
{
// 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:\n";
*intOut = 12;
*intOut = 21;
// no effect on iterator position
intOut++;
*intOut = 9;
*intOut = 7;
return 0;
}
The template class ostreambuf_iterator describes an output iterator object that writes successive character elements onto the output stream with the extraction operator >>.
Theostreambuf_iterators differ from those of theostream_iterator Class in having characters instead of a generic type at the type of object being inserted into the output stream.
template <
class CharType = char
class Traits = char_traits<CharType> >
Parameter | Description |
CharType | The type that represents the character type for the ostreambuf_iterator. This argument is optional and the default value is char. |
Traits | The type that represents the character type for the ostreambuf_iterator. This argument is optional and the default value is char_traits<CharType>. |
Table 32.19 |
Theostreambuf_iterator class must satisfy the requirements for an output iterator. Algorithms can be written directly to output streams using an ostreambuf_iterator.
The class provides a low-level stream iterator that allows access to the raw (unformatted) I/O stream in the form of characters and the ability to bypass the buffering and character translations associated with the high-level stream iterators.
Typedef | Description |
char_type | A type that provides for the character type of the ostreambuf_iterator. |
ostream_type | A type that provides for the stream type of the ostream_iterator. |
streambuf_type | A type that provides for the stream type of the ostreambuf_iterator. |
traits_type | A type that provides for the character traits type of the ostream_iterator. |
Table 32.20 |
Constructs an ostreambuf_iterator that is initialized to write characters to the output stream.
ostreambuf_iterator(streambuf_type* _Strbuf) throw();
ostreambuf_iterator(ostream_type& _Ostr) throw();
Parameter | Description |
_Strbuf | The outputstreambuf object used to initialize the output stream-buffer pointer. |
_Ostr | The output stream object used to initialize the output stream-buffer pointer. |
Table 32.21 |
The first constructor initializes the output stream-buffer pointer with_Strbuf.
The second constructor initializes the output stream-buffer pointer with_Ostr.rdbuf.
The stored pointer must not be a null pointer.
// ostreambuf_iterator, ostreambuf_iterator
#include <iterator>
#include <vector>
#include <iostream>
using namespace std;
int main()
{
// ostreambuf_iterator for stream cout
ostreambuf_iterator<char> charOut(cout);
*charOut = '7';
charOut ++;
*charOut = 'T';
charOut ++;
*charOut = 'W';
cout<<" are characters output."<<endl;
ostreambuf_iterator<char> strOut(cout);
string str = "These characters are being written to the output stream.\n ";
copy(str.begin(), str.end(), strOut);
return 0;
}
Member function | Description |
failed() | Tests for failure of an insertion into the output stream buffer. |
ostreambuf_iterator | Constructs an ostreambuf_iterator that is initialized to write characters to the output stream. |
Table 32.22 |
Tests for failure of an insertion into the output stream buffer.
bool failed() const throw();
The return value is true if no insertion into the output stream buffer has failed earlier; otherwise false.
The member function returns true if, in any prior use of memberoperator=, the call tosubf_→sputc returned eof.
// ostreambuf_iterator, failed()
#include <iterator>
#include <vector>
#include <iostream>
using namespace std;
int main()
{
// ostreambuf_iterator for stream cout
ostreambuf_iterator<char> charOut(cout);
*charOut = 'T';
charOut ++;
*charOut = '7';
charOut ++;
*charOut = 'R';
cout<<" are characters output"<<endl;
bool bol = charOut.failed();
if(bol)
cout<<"At least one insertion failed."<<endl;
else
cout<<"No insertions failed."<<endl;
return 0;
}
Operator | Description |
operator* | Dereferencing operator used to implement the output iterator expression such as *i = x. |
operator++ | A nonfunctional increment operator that returns an ostreambuf_iterator to the same object it addressed before the operation was called. |
operator= | The operator inserts a character into the associated stream buffer. |
Table 32.23 |
The source code for this tutorial is available inC++ STL Iterator source code samples.
Acomplete C++ Standard Library documentation that includes STL.