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.
| 1.1 Continuation from the previous Module
The insert_iterator::operator++
insert_iterator& operator++();insert_iterator& operator++(int);
Parameters
// 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; }
|

Assignment operator used to implement the output iterator expression such as*i = x.
insert_iterator& operator=(typename Container::const_reference _Val);
Parameter | Description |
_Val | The value to be assigned to the element. |
Table 32.1 | |
The return value is a reference to the element inserted into the container.
The member function evaluates Iter = container. insert(Iter, _Val), then returns *this pointer
// 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;
}

Describes an input iterator object. It extracts objects of classType from an input stream, which it accesses through an object it stores, of type pointer to basic_istream<CharType, Traits>.
template < class Type class CharType = char class Traits = char_traits<CharType> class Distance= ptrdiff_t >
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 | |
After constructing or incrementing an object of class istream_iterator with a non null stored pointer, the object attempts to extract and store an object of type Type from the associated input stream.
If the extraction fails, the object effectively replaces the stored pointer with a null pointer, thus making an end-of-sequence indicator.
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
typedef CharType char_type;
The istream_iterator::traits_type
typedef Traits traits_type;
|
// 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;
}

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 | |
Constructs either an end-of-stream iterator as the default istream_iterator or a istream_iterator initialized to the iterator's stream type from which it reads. The constructor prototypes are:
istream_iterator();
istream_iterator(istream_type& _Istr);
Parameter | Description |
_Istr | The input stream to be read use to initialize the istream_iterator. |
Table 32.5 | |
The first constructor initializes the input stream pointer with a null pointer and creates an end-of-stream iterator.
The second constructor initializes the input stream pointer with&_Istr, then attempts to extract and store an object of type Type.
The end-of-stream iterator can be use to test whether anistream_iterator has reached the end of a stream.
// 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;
}

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 template class istreambuf_iterator describes an input iterator object that extracts character elements from an input stream buffer, which it accesses through an object it stores, of type pointer to basic_streambuf<CharType, Traits>.
template < class CharType class Traits = char_traits<CharType> >
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 | |
Theostreambuf_iterator class must satisfy the requirements for an input iterator.
After constructing or incrementing an object of class istreambuf_iterator with a non-null stored pointer, the object effectively attempts to extract and store an object of type CharType from the associated input stream.
The extraction may be delayed, however, until the object is actually dereferenced or copied. If the extraction fails, the object effectively replaces the stored pointer with a null pointer, thus making an end-of-sequence indicator.
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 | |
A type that provides for the character type of the ostreambuf_iterator.
typedef CharType char_type;The type is a synonym for the template parameter CharType.
// 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;
}

A type that provides an integer type for an istreambuf_iterator.
typedef typename Traits::int_type int_type;The type is a synonym for Traits::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;
}

The source code for this tutorial is available inC++ STL Iterator source code samples.
Acomplete C++ Standard Library documentation that includes STL.