Formatting output with C++

In order to explore the language, including new features, I make an effort to implement C++ solutions to the Perl Weekly Challenge

These challenges are generally designed to be short and take advantage of the high level features that a scripting language such as Perl offers. It has been nice to find that modern C++ offers features, mostly through STL and Boost, that allow for code that is not much longer than would be required in a scripting language. The main difference is usually just that in C++ we must, of course, pay attention to types.

Solutions in Perl and Raku are here.

Part 1

Sample Run

$ g++ -o cxx/ch-1 cxx/ch-1.cxx
$ echo abracadabra | cxx/ch-1
a: 5
b: 2
c: 1
d: 1
r: 2

Part 2

Sample Run


  • In Part 1 we are asked to keep a count of the number of times letters are used in some text. This code follows pretty directly from my Perl solution to this problem: iterate over each character in the string, ignore whitespaces and punctuation, and store a count for each character in a map.
  • Part 2 involved mostly formatting output into a nice table. Perl offers some nice report templating abilities but doing it slightly more manually C++ style by setting field widths and left/right justifying is not much more work. In fact, the number of lines of code required to print the same table was equal in all of Perl, Raku, and C++.

Comments for this post were locked by the author