|< C++ STL Iterator 4 | Main | C++ STL Iterator 6 >| Site Index | Download |


 

 

 

 

MODULE 32

THE C++ STL ADVANCED ITERATOR PART 5

 

 

 

 

 

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 skills and knowledge that supposed to be acquired:

 

 

 

 

 

 

 

 

 

 

 

 

 

What do we have in this session?

  1. The insert_iterator::operator++

  2. insert_iterator, operator++ program example

  3. The insert_iterator::operator= prototype

  4. The istream_iterator Template Class prototype

  5. The istream_iterator::char_type

  6. The istream_iterator::traits_type

  7. istream_iterator, char_type and traits_type program example

  8. The istream_iterator::istream_iterator

  9. istream_iterator, istream_iterator program example

  10. The istreambuf_iterator Template Class

  11. The istreambuf_iterator Template Class Members

  12. The istreambuf_iterator::char_type

  13. istreambuf_iterator, char_type program example

  14. The istreambuf_iterator::int_type

  15. istreambuf_iterator, int_type program example

 

 

 

 

1.1     Continuation from the previous Module

 

The insert_iterator::operator++

  • Increments the insert_iterator to the next location into which a value may be stored.

insert_iterator& operator++();
insert_iterator& operator++(int);

 

Parameters

  • An insert_iterator addressing the next location into which a value may be stored.

  • Both pre-incrementation and post-incrementation operators return the same result.

// insert_iterator, operator++

//the increment...

#include <iterator>

#include <vector>

#include <iostream>

using namespace std;

 

int main()

{

    int i;

    vector<int> vec;

    for(i = 10; i<=15; ++i)

        vec.push_back(i);

    vector <int>::iterator veciter;

    cout<<"The vector vec data: ";

    // iterate all the elements and print...

    for(veciter = vec.begin(); veciter != vec.end(); veciter++)

        cout<<*veciter<<" ";

    cout<<endl;

    cout<<"\nOperation: j(vec, vec.begin()) then *j = 17 and j++...\n";

    insert_iterator<vector<int> > j(vec, vec.begin());

    *j = 17;

    j++;

    *j = 9;

    cout<<"After the insertions, the vector vec data:\n";

    for(veciter = vec.begin(); veciter != vec.end(); veciter++)

        cout<<*veciter<<" ";

    cout<<endl;

    return 0;

}

 

Output:

C++ STL Iterator insert_iterator

 

The insert_iterator::operator= prototype

insert_iterator& operator=(typename Container::const_reference _Val);

 

Parameter

 

Parameter

Description

_Val

The value to be assigned to the element.

 

Table 32.1

// insert_iterator, operator= the assignment

#include <iterator>

#include <list>

#include <iostream>

using namespace std;

 

int main()

{

    int i;

    list<int>::iterator lstiter;

    list<int> lst;

    for(i = 10; i<=15; ++i)

        lst.push_back(i);

    cout<<"The list lst data: ";

    for(lstiter = lst.begin(); lstiter != lst.end(); lstiter++)

        cout<<*lstiter<<" ";

    cout<<endl;

    insert_iterator< list < int> > Iter(lst, lst.begin());

    *Iter = 12;

    *Iter = 7;

    *Iter = 33;

    *Iter = 24;

    cout<<"\nOperation: Iter(lst, lst.begin()) then *Iter = 12...\n";

    cout<<"After the insertions, the list lst data:\n";

    for(lstiter = lst.begin(); lstiter != lst.end(); lstiter++)

        cout<<*lstiter<<" ";

    cout<<endl;

    return 0;

}

 

Output:

 

C++ STL Iterator insert_iterator operator=

 

The istream_iterator Template Class prototype

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

 

Parameters

 

Parameter

Description

Type

The type of object to be extracted from the input stream.

CharType

The type that represents the character type for the istream_iterator. This argument is optional and the default value is char.

Traits

The type that represents the character type for the istream_iterator. This argument is optional and the default value is char_traits<CharType>.

Distance

A signed integral type that represents the difference type for the istream_iterator. This argument is optional and the default value is ptrdiff_t.

 

Table 32.2

The istream_iterator Template Class Members

 

Typedefs

 

Typedef

Description

char_type

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

istream_type

A type that provides for the stream type of the istream_iterator.

traits_type

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

 

Table 32.3

 

The istream_iterator::char_type

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

typedef CharType char_type;
  • The type is a synonym for the template parameter CharType.

 

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.

 

// istream_iterator, char_type and traits_type

#include <iterator>

#include <vector>

#include <iostream>

using namespace std;

 

int main()

{

    typedef istream_iterator<int>::char_type chtype;

    typedef istream_iterator<int>::traits_type tratype;

    // standard iterator interface for reading elements from the input stream...

    cout<<"Enter integers separated by spaces & then\nany character e.g.: '3 4 7 T': ";

    // istream_iterator for reading int stream

    istream_iterator<int, chtype, tratype> intread(cin);

    // end-of-stream iterator

    istream_iterator<int, chtype, tratype> EOFintread;

    while(intread != EOFintread)

    {

        cout<<"Reading data:  "<<*intread<<endl;

        ++intread;

    }

    cout<<endl;

    return 0;

}

 

Output:

 

C++ STL Iterator istream_iterator, char_type and traits_type

 

Member Functions

 

Member function

Description

istream_iterator

Constructs either an end-of-stream iterator as the default istream_iterator or istream_iterator initialized to the iterator's stream type from which it reads.

 

Table 32.4

 

The istream_iterator::istream_iterator

  1. istream_iterator();

  2. istream_iterator(istream_type& _Istr);

Parameter

 

Parameter

Description

_Istr

The input stream to be read use to initialize the istream_iterator.

 

Table 32.5

// istream_iterator, istream_iterator

#include <iterator>

#include <vector>

#include <algorithm>

#include <iostream>

using namespace std;

 

int main()

{

    // used in conjunction with copy algorithm to put elements into a vector read from cin

    vector<int> vec(5);

    vector<int>::iterator Iter;

    cout<<"Enter 5 integers separated by spaces & then\na character e.g: '4 6 2 7 11 R': ";

    istream_iterator<int> intvecread(cin);

    // default constructor will test equal to end of stream for delimiting source range of vector

    copy(intvecread, istream_iterator<int>(), vec.begin());

    cin.clear();

    cout<<"vec data: ";

    for(Iter = vec.begin(); Iter != vec.end(); Iter++)

        cout<<*Iter<<" ";

    cout<<endl;

    return 0;

}

 

Output:

 

C++ STL Iterator istream_iterator, istream_iterator

 

Operators

 

Operator

Description

operator*

The dereferencing operator returns the stored object of type Type addressed by the istream_iterator.

operator->

Returns the value of a member, if any.

operator++

Either extracts an incremented object from the input stream or copies the object before incrementing it and returns the copy.

 

Table 32.6

 

The istreambuf_iterator Template Class

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

 

Parameters

 

Parameter

Description

CharType

The type that represents the character type for the istreambuf_iterator.

Traits

The type that represents the character type for the istreambuf_iterator. This argument is optional and the default value is char_traits<CharType>.

 

Table 32.7

The istreambuf_iterator Template Class Members

 

Typedefs

 

Typedef

Description

char_type

A type that provides for the character type of the ostreambuf_iterator.

int_type

A type that provides an integer type for an istreambuf_iterator.

istream_type

A type that provides for the stream type of the istream_iterator.

streambuf_type

A type that provides for the stream type of the istreambuf_iterator.

traits_type

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

 

Table 32.8

 

The istreambuf_iterator::char_type

typedef CharType char_type;

// istreambuf_iterator, char_type

#include <iterator>

#include <vector>

#include <iostream>

#include <algorithm>

using namespace std;

 

int main()

{

    typedef istreambuf_iterator<char>::char_type chatype;

    typedef istreambuf_iterator<char>::traits_type tratype;

    cout<<"Enter line of text, then press Return key to \n"

            <<"insert into the output, & use a ctrl-Z Enter key\ncombination to exit: ";

    // istreambuf_iterator for input stream

    istreambuf_iterator< chatype, tratype> charInBuf(cin);

    ostreambuf_iterator<char> charOut(cout);

    // used in conjunction with replace_copy algorithm to insert into output stream and replaces spaces with hash sign

    replace_copy(charInBuf, istreambuf_iterator<char>(), charOut, ' ', '#');

    return 0;

}

 

Output:

 

C++ STL Iterator istreambuf_iterator, char_type

 

The istreambuf_iterator::int_type

typedef typename Traits::int_type int_type;

// istreambuf_iterator, int_type

#include <iterator>

#include <iostream>

using namespace std;

 

int main()

{

    cout<<"Operation: int_type intype = 77\n";

    istreambuf_iterator<char>::int_type intype = 77;

    cout<<"The int_type type = "<<intype<<endl;

    return 0;

}

 

Output:

 

C++ STL Iterator istreambuf_iterator, int_type

 

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 4 | Main | C++ STL Iterator 6 >| Site Index | Download |


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