TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C++ Output Iterator

C++ Output Iterator with C++ tutorial for beginners and professionals with examples on constructor, if-else, switch, break, continue, comments, arrays, object and class, exception, static, structs, inheritance, aggregation etc.

<< Back to CPP

C++ Output Iterator

  • 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
C++ Output 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

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

Parameters

x: It is the container on which the new element is to be inserted.

it: It is an iterator object pointing to the position which is to be modified.

Let's see a simple 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:

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

In the above example, insert_iterator is applied on the copy algorithm to insert the elements of the vector v2 into the vector v1 at a specified position pointed 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

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

Member functions of Ostream Iterator class

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:

#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:

10,20,30,40,50

In the above example, out is an object of the ostream_iterator used to add the delimiter ',' between the vector elements.

Let's see another simple example of ostream iterator:

#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:

5,10,15,

Features Of Output Iterator

  • Equality/Inequality Operator: Output iterators cannot be compared either by using equality or inequality operator. Suppose X and Y are the two iterators:
X==Y;  invalid
X!=Y;   invalid
  • Dereferencing: An output iterator can be dereferenced as an lvalue.
*X=7;
  • Incrementable: An output iterator can be incremented by using operator++() function.
X++;
++X;

Limitations Of Output Iterator

  • Assigning but no accessing: We can assign an output iterator as an lvalue, but we cannot access them as an rvalue.

Suppose 'A' is an output iterator type and 'x' is a integer variable:

*A = x;                   // valid
  x = *A;                 // invalid
  • It cannot be decremented: We can increment the output iterator by using operator++() function, but we cannot decrement the output iterator.

Suppose 'A' is an output iterator type:

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:

A = =B;        // not valid
A = =B;        // not valid
  • Arithmetic Operators: An output iterator cannot be used with the arithmetic operators. Therefore, we can say that the output iterator only moves forward in a sequential manner.

Suppose 'A' is an output iterator:

A + 2;               // invalid
A + 5;               // invalid   

Next Topic




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf