My Byte of Code Blog. Tips and observations on creating software with Objective-C, C/C++, Python, Cocoa and Boost on Mac.

Wednesday, February 24, 2010

Parse CSV File With Boost Tokenizer In C++

Often data required by application are available in CSV formatted files. In c++ it is easy to read file line by line. All that is left is to extract fields from each line and insert them into datastructure stored in memory. Boost Tokenizer is a package that provides a way to easilly break a string or sequence of characters into sequence of tokens, and provides standard iterator interface to traverse the tokens. I will show simple way of using Boost Tokenizer to parse data from CSV file.

Tuesday, February 23, 2010

My FizzBuzz Solution In C++

If you are not sure if I can write simple code here is my FizzBuzz c++ solution:

#include <iostream>
using namespace std;

int main()
{
   for (int i = 1; i < 101; ++i)
   {
       if      (i % 3 == 0 && i % 5 == 0) cout << "FizzBuzz";
       else if (i % 3 == 0)               cout << "Fizz";
       else if (i % 5 == 0)               cout << "Buzz";
       else                               cout << i;
       cout << endl;
   }
}

Monday, February 08, 2010

Representing bidirectional relationship with boost::bimap

In C++, std maps and hash-maps are perfect choice for storing key-value pairs of elements. We use key to lookup related value(s). Quite often I manipulate key-value relationships where both keys and values are unique and usually there is a one-to-one relationships between keys and values. Example of this is a translation tables between two sets of symbols (strings).

I ended up coding my own class with two internal maps, or hash-maps, plus getters and setters to populate and access values for two way lookups.

A while ago I stumbled upon boost's bimap. Here is an example how to use it.

Friday, February 05, 2010

Use vim to generate and execute bash commands

I have been using vim almost exclusively for file editing on any operating system that I use, whether Mac OS X, Linux or Windows. Here is an example how to use vim to build and execute bash commands using vim's regular expressions.