C++ Output Iterator

In C++, iterators are used to traverse and manipulate elements in a container. Output iterators are a specific type of iterator that allows us to write values into a container. They are primarily used for output operations, such as writing data to a file or printing to the console.

To understand output iterators, let’s explore their characteristics and how they can be used in practice.

Characteristics of Output Iterators

Output iterators have the following characteristics:

  • They support the assignment operator (=) to write values.
  • They support the pre-increment operator (++it) to move to the next position.
  • They do not support the equality operator (==) for comparison.
  • They do not support arithmetic operations like addition or subtraction.

These characteristics distinguish output iterators from other types of iterators in C++.

Example: Writing to a File Using Output Iterator

Let’s consider an example where we want to write a list of numbers to a file using an output iterator. We’ll start by including the necessary header files:

#include <iostream>
#include <fstream>
#include <iterator>

Next, we’ll open a file for writing:

std::ofstream outputFile("output.txt");

Now, we can create an output iterator and use it to write values to the file:

std::ostream_iterator<int> outputIterator(outputFile, "n");
*outputIterator = 10;
++outputIterator;
*outputIterator = 20;
++outputIterator;
*outputIterator = 30;

In the above code, we create an output iterator of type `std::ostream_iterator`, which takes the output file stream `outputFile` as its first parameter and the delimiter `”n”` as its second parameter. We then assign values to the iterator using the assignment operator (=) and move to the next position using the pre-increment operator (++it).

After executing the above code, the file “output.txt” will contain the numbers 10, 20, and 30, each on a new line.

Example: Printing to the Console Using Output Iterator

Another common use case for output iterators is printing values to the console. Let’s consider an example where we have a vector of strings and we want to print each string to the console using an output iterator:

#include <iostream>
#include <vector>
#include <iterator>

Next, we’ll define a vector of strings:

std::vector<std::string> strings = {"Hello", "World", "C++"};

Now, we can create an output iterator and use it to print each string to the console:

std::ostream_iterator<std::string> outputIterator(std::cout, " ");
for (const auto& str : strings) {
  *outputIterator = str;
  ++outputIterator;
}

In the above code, we create an output iterator of type `std::ostream_iterator`, which takes `std::cout` (the standard output stream) as its first parameter and the delimiter `” “` as its second parameter. We then iterate over each string in the vector using a range-based for loop, assign each string to the iterator, and move to the next position using the pre-increment operator (++it).

After executing the above code, the console will display the strings “Hello”, “World”, and “C++” separated by spaces.

Conclusion

Output iterators in C++ are a powerful tool for writing values into containers, whether it’s writing to a file or printing to the console. They have specific characteristics that distinguish them from other types of iterators, such as supporting the assignment operator (=) and the pre-increment operator (++it). Understanding how to use output iterators effectively can greatly enhance your ability to manipulate and output data in C++.

Scroll to Top