O'Reilly logo

C++17 STL Cookbook by Jacek Galowicz

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How to do it...

We will play with std::ostream_iterator by enabling for combination with a new custom class and have a look into its implicit conversion capabilities, which can help us with printing:

  1. The include files come first and then we declare that we use the std namespace by default:
      #include <iostream>      #include <vector>      #include <iterator>      #include <unordered_map>      #include <algorithm>            using namespace std;
  1. Let's implement a transformation function, which maps numbers to strings. It shall return "one" for the value 1, "two" for the value 2, and so on:
      string word_num(int i) {
  1. We fill a hash map with the mappings we need in order to access them later:
          unordered_map<int, string> m {              {1, "one"}, {2, "two"}, {3, "three"}, {4, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required