Home >>C++ Standard Template Library Tutorial(STL) >C++ Output Iterator
An Output Iterator generally have two main subclasses that are as depicted below:
An iterator that is generally used to insert the element in a specified position is known as the insert iterator. The new element is inserted on the insert_iterator by an assignment operator at the current position.
Syntax
Here is the syntax of the input iterator:
template<class Container, class Iterator>
insert_iterator<container> inserter(Container &x,Iterator it);
Here are the parameters of the syntax of the insert iterator:
x: The new element is generally inserted in this container.
it: An iterator object that is generally pointing towards the position that is to be modified is known as it.
Here is an example of the insert iterator that will help you grasp the more of it from a deeper level:
#include <iostream> #include <iterator> #include <vector> #include <algorithm> using namespace std; int main () { vector<int> vect1,vect2; for (int i=1; i<=5; i++) { vect1.push_back(i); vect2.push_back(i+2); } vector<int>::iterator it = vect1.begin(); advance (it,3); copy (vect2.begin(),vect2.end(),inserter(vect1,it)); cout<<"Elements of vect1 are :"; for ( it = vect1.begin(); it!= vect1.end(); ++it ) cout << ' ' << *it; cout << '\n'; return 0; }
The output iterators that are generally used to write to the output stream like cout successively are known as the ostream iterator. By the use of the basic_ostream object an ostream iterator is created. A new element is generally inserted into the output stream, whenever an assignment operator is implemented on the ostream operator.
Syntax
Here is the syntax of the ostream iterator:
template<class T, class charT=char, class traits=char_traits<charT>> class ostream_iterator; Here are the member functions of Ostream Iterator class:
Here are the parameters of the syntax of the ostream iterator:
Here is an example of the ostream iterator that will help you in understanding the method easily:
#include <iostream> #include<iterator> #include<vector> #include<algorithm> using namespace std; int main() { vector<int> vect; for(int i=1;i<=5;i++) { vect.push_back(i*10); } ostream_iterator<int> out(cout,","); copy(vect.begin(),vect.end(),out); return 0; }
Here is another simple example of the ostream iterator:
#include <iostream> #include<iterator> #include<vector> #include<algorithm> using namespace std; int main() { ostream_iterator<int> out1(cout,","); *out1 = 10; out1++; *out1 = 20; out1++; *out1 = 30; return 0; }
Here are the excellent features that are provided by the Output iterator.
Here are the limitations that generally apply to the output Iterator: