|< C++ STL Iterator 5 | Main | C++ STL Iterator 7 >| Site Index | Download |


 

 

 

 

 

 

MODULE 32_1

THE C++ STL ADVANCED ITERATOR PART 6

 

 

 

 

 

 

My Training Period: xx hours

 

The program examples for the member used functions compiled using VC++7.0 / .Net, win32 empty console mode application.  This is a continuation from the previous Module. Linux g++ compilation examples given at the end of this Module. The source code for this tutorial is available in C++ STL Iterator source code samples.

 

The C++ STL iterators knowledge that supposed to be acquired:

 

 

 

 

 

 

 

 

 

 

 

 

What do we have in this page?

  1. The istream_iterator::traits_type

  2. The istreambuf_iterator::equal

  3. istreambuf_iterator, equal program example

  4. The istreambuf_iterator::istreambuf_iterator

  5. istreambuf_iterator, istreambuf_iterator program example

  6. The istreambuf_iterator::operator++

  7. The ostream_iterator Template Class

  8. The ostream_iterator Template Class Members

  9. The ostream_iterator::ostream_iterator

  10. ostream_iterator, ostream_iterator program example

  11. The ostream_iterator::operator=

  12. ostream_iterator, operator= program example

  13. The ostreambuf_iterator Template Class

  14. The ostreambuf_iterator Template Class Members

  15. The ostreambuf_iterator::ostreambuf_iterator

  16. ostreambuf_iterator, ostreambuf_iterator program example

  17. The ostreambuf_iterator::failed

  18. ostreambuf_iterator, failed() program example

 

 

 

 

The istream_iterator::traits_type

  • A type that provides for the character traits type of the istream_iterator.

typedef Traits traits_type;
  • The type is a synonym for the template parameter Traits.

Member Functions

 

Member function

Description

equal()

Tests for equality between two input stream buffer iterators.

istreambuf_iterator

Constructs an istreambuf_iterator that is initialized to read characters from the input stream.

 

Table 32.9

 

The istreambuf_iterator::equal

  • Tests for equivalence between two input stream buffer iterators.

bool equal(const istreambuf_iterator& _Right) const;

 

Parameter

 

Parameter

Description

_Right

The iterator for which to check for equality.

 

Table 32.10

  • The return value is true if both istreambuf_iterators are end-of-stream iterators or if neither is an end-of-stream iterator; otherwise false.

  • A range is defined by the istreambuf_iterator to the current position and the end-of-stream iterator, but since all non-end-of stream iterators are equivalent under the equal() member function, it is not possible to define any sub-ranges using istreambuf_iterators.  The == and != operators have the same semantics.

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

}

 

Output:

 

C++ STL Iterator istreambuf_iterator equal

 

The istreambuf_iterator::istreambuf_iterator

  1. istreambuf_iterator ( streambuf_type* _Strbuf = 0 ) throw();

  2. istreambuf_iterator ( istream_type& _Istr ) throw();

 

Parameters

 

Parameter

Description

_Strbuf

The input stream buffer to which the istreambuf_iterator is being attached.

_Istr

The input stream to which the istreambuf_iterator is being attached.

 

Table 32.11

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

 

Output:

 

C++ STL Iterator istreambuf_iterator

 

Operators

 

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

 

The istreambuf_iterator::operator++

istreambuf_iterator& operator++();

istreambuf_iterator operator++(int);

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

}

 

Output:

 

C++ STL Iterator istreambuf_iterator operator++

 

The ostream_iterator Template Class

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

 

Parameters

 

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

The ostream_iterator Template Class Members

 

Typedefs

 

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

  • Constructs an ostream_iterator that is initialized and delimited to write to the output stream.

  1. ostream_iterator(ostream_type& _Ostr);

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

Parameters

 

Parameter

Description

_Ostr

The output stream object used to initialize the output stream pointer.

_Delimiter

The output stream delimiter used to initialize the output stream pointer.

 

Table 32.15

  • The first constructor initializes the output stream pointer with &_Ostr. The delimiter string pointer designates an empty string.

  • The second constructor initializes the output stream pointer with &_Ostr and the delimiter string pointer with _Delimiter.

 

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

}

 

Output:

 

C++ STL Iterator ostream_iterator, ostream_iterator

 

Member Functions

 

Member function

Description

ostream_iterator

Constructs an ostream_iterator that is initialized and delimited to write to the output stream.

 

Table 32.16

 

Operators

 

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

 

The ostream_iterator::operator=

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

Parameter

 

Parameter

Description

_Val

The value of the object of type Type to be inserted into the output stream.

 

Table 32.18

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

}

 

Output:

 

 

C++ STL Iterator ostream_iterator operator=

 

The ostreambuf_iterator Template Class

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

 

Parameters

 

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

The ostreambuf_iterator Template Class Members

 

Typedefs

 

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

 

The ostreambuf_iterator::ostreambuf_iterator

  1. ostreambuf_iterator(streambuf_type* _Strbuf) throw();

  2. ostreambuf_iterator(ostream_type& _Ostr) throw();

Parameters

 

Parameter

Description

_Strbuf

The output streambuf 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

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

}

 

Output:

 

C++ STL Iterator ostreambuf_iterator

 

Member Functions

 

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

 

The ostreambuf_iterator::failed

bool failed() const throw();

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

}

 

Output:

 

C++ STL Iterator ostreambuf_iterator failed()

 

Operators

 

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

 

 

 

tenouk C++ STL tutorial

 

 

 

 

 

 

 

 

 

 

 

Further C++ STL iterators related reading:

 

  1. C++ Templates programming tutorials.

  2. The source code for this tutorial is available in C++ STL Iterator source code samples.

  3. A complete C & C++ Standard Library documentation that includes STL.

  4. Check the best selling C / C++ and STL books at Amazon.com.

 

 

 

 

 

 

 

|< C++ STL Iterator 5 | Main | C++ STL Iterator 7 >| Site Index | Download |


C++ STL Iterators Classes:  Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7