Boost logo

Boost Users :

From: Caleb Epstein (caleb.epstein_at_[hidden])
Date: 2004-08-24 13:08:33


On Mon, 23 Aug 2004 08:42:09 -0700, Steven L. Scott <sls_at_[hidden]> wrote:

> I'm working on an application (a data analysis system built in C++) for
> which it would be helpful to have nested (multidimensional) containers
> other than ordinary arrays.

I've been toying with using boost::variant for something like this -
an arbitrarily nested data structure that could be used like the
hashes in Perl/Python etc. Unfortunately, because boost::variant
doesn't know about the std::map operators, you can't use code like
thedata["first"]["second"] but you can write something like this
(data_t is used just to gain the syntactic sugar of
std::map::operator[])

#include <map>
#include <string>
#include <boost/variant.hpp>

typedef boost::make_recursive_variant<
    int,
    double,
    std::string,
    std::map<std::string, boost::recursive_variant_>
>::type atom_t;

typedef std::map<std::string, atom_t> data_t;

int main ()
{
    data_t scores, student_scores;

    student_scores["Midterm"] = 90.0;
    student_scores["Final"] = 88.0;
    scores["johnny"] = student_scores;
}

I'd love to get some feedback on this approach, especially if anyone
has a solution for implementing data["foo"]["bar"] etc.

-- 
Caleb Epstein
caleb.epstein_at_[hidden]

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net