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:
- 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;
- 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) {
- 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, ...