C++ Output Iterator - C++ Programming Tutorial
C++ Course / Miscellaneous / C++ Output Iterator

C++ Output Iterator

BLUF: Mastering C++ Output Iterator is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: C++ Output Iterator

C++ is renowned for its efficiency. Learn how C++ Output Iterator enables low-level control and high-performance computing in the tutorial below.

  • Output Iterator is an iterator used to modify the value in the container.
  • Dereferencing an output iterator allows us to alter the value of the container.
  • It does not allow us to read the value from the container.
  • It is a one-way and write-only iterator.
  • It can be incremented, but cannot be decremented.
  • Operators that can be used for an output iterator are increment operator(++), decrement operator(--) and assignment operator(=) .
  • There are two main subclasses of an Output Iterator are: insert iterator ostream iterator
  • insert iterator
  • ostream iterator
  • Insert Iterator

  • An insert iterator is an iterator used to insert the element in a specified position.
  • An assignment operator on the insert_iterator inserts the new element at the current position.
  • Syntax

Example

template<class Container, class Iterator>

insert_iterator<container> inserter(Container &x,Iterator it);

Parameters

x : This is the element where the new item will be added.

It refers to an iterator object pointing to the position that needs modification.

Let's see a simple example:

Example

#include <iostream>     // std::cout

#include <iterator>     // std::front_inserter

#include <vector>         // std::list

#include <algorithm>    // std::copy

using namespace std;

int main () {

  vector<int> v1,v2;

  for (int i=1; i<=5; i++)

  { 

  v1.push_back(i); 

  v2.push_back(i+2);

  }

 vector<int>::iterator it = v1.begin();

  advance (it,3);

 copy (v2.begin(),v2.end(),inserter(v1,it));

  cout<<"Elements of v1 are :";

  for (  it = v1.begin(); it!= v1.end(); ++it )

  cout << ' ' << *it;

  cout << '\n';

  return 0;

}

Output:

Output

Elements of v1 are : 1 2 3 3 4 5 6 7 4 5

In the previously mentioned example, the insert_iterator is utilized with the copy algorithm to add the elements from the vector v2 into the vector v1 at a designated position indicated by it.

Ostream iterator

  • An ostream iterators are the output iterators used to write to the output stream such as cout successively.
  • An ostream iterator is created using a basic_ostream object.
  • When an assigenment operator is used on the ostream iterator, it inserts a new element into the output stream.
  • Syntax

Example

template<class T, class charT=char, class traits=char_traits<charT>>

class ostream_iterator;

Member functions of Ostream Iterator class

Example

Ostream_iterator<T, charT, traits>& operator=(const T& value);

Ostream_iterator<T, charT, traits>& operator*();

Ostream_iterator<T, charT, traits>& operator++();

Ostream_iterator<T, charT, traits>& operator++(int);

Parameters

  • T : It is the type of elements to be inserted into the container.
  • charT : The type of elements that ostream can handle, for example, char ostream.
  • traits : These are the character traits that the stream handles for the elements.

Let's see a simple example:

Example

#include <iostream>

#include<iterator>

#include<vector>

#include<algorithm>

using namespace std;

int main()

{

   vector<int> v;

   for(int i=1;i<=5;i++)

   {

       v.push_back(i*10);

   }

 ostream_iterator<int> out(cout,",");

 copy(v.begin(),v.end(),out);

    return 0;

}

Output:

Output

10,20,30,40,50

In the provided illustration, 'out' represents an instance of the ostream_iterator utilized to insert the delimiter ',' amidst the elements within the vector.

Let's explore another straightforward instance of ostream iterator:

Example

#include <iostream>

#include<iterator>

#include<vector>

#include<algorithm>

using namespace std;

int main()

{

   ostream_iterator<int> out(cout,",");

   *out = 5;

   out++;

   *out = 10;

   out++;

   *out = 15;

   return 0;

}

Output:

Output

5,10,15,

Comparison Operator: Output iterators are not allowed to be compared using either the equality or inequality operator. Let's consider X and Y as two iterators:

Example

X==Y;  invalid

X!=Y;   invalid
  • Dereferencing : An output iterator can be dereferenced as an lvalue.
  • Incrementable : An output iterator can be incremented by using operator++ function.
Example

X++;

++X;

Assigning without accessing: It is possible to assign an output iterator as an lvalue, but it is not allowed to access them as an rvalue.

Suppose 'A' represents an output iterator type and 'x' denotes an integer variable:

Example

*A = x;                   // valid

  x = *A;                 // invalid
  • It is not possible to decrement it: While we can increase the output iterator using the operator++ function, decreasing the output iterator is not supported.

Suppose 'A' is an output iterator type:

Example

A++;            // not valid

++A;            // not valid
  • Multi-pass algorithm : An output iterator cannot be used as a multi-pass algorithm. Since an output iterator is unidirectional and can move only forward. Therefore, it cannot be used to move through the container multiple times
  • Relational Operators : An output iterator cannot be compared by using any of the relational operators.

Suppose 'A' and 'B' are the two iterators:

An output iterator is incompatible with arithmetic operators, limiting its functionality to only advancing in a sequential order.

Suppose 'A' is an output iterator :

Example

A + 2;               // invalid

A + 5;               // invalid

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience