Home > Uncategorized > C++ CSV parser – Reaching for perfection

C++ CSV parser – Reaching for perfection

Digging down into split_iterators and failing to combine these with istream_iterators I took one step back and ended up with a piece of code that I think is both clear and efficient. Look at this:

#include <iostream>

#include <string>

#include <vector>

#include <algorithm>

#include <boost/lexical_cast.hpp>

#include <boost/algorithm/string.hpp>

 

template<class T>

std::vector<std::vector<T>> ParseCsv(std::istream& is)

{

    using namespace std;

    using namespace boost;

    typedef iterator_range<string::iterator> range_t;

 

    vector<vector<T>> result;

    string row;

    while(getline(is, row))

    {

        result.push_back(vector<T>());

        vector<range_t> values;

        split(values, row, is_any_of(","), token_compress_on);

        transform(values.begin(), values.end(), back_inserter(result.back()), [](range_t& r) {

            return boost::lexical_cast<T>(std::string(r.begin(), r.end()));

        });

    }

 

    return result;

}

 

Usage:

vector<vector<int>> result = ParseCsv<int>(ifstream("test.csv"));

 

Nice. Seems like boost is essential nowadays for C++ programming. Can’t live without it.

Advertisements
Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: