From Python to c++

M

Marco Aschwanden

This is actually a c++ problem. Got no satisfying answer on comp.lang.c++.
Maybe you can help better because I know, there are many c++-converts ;).

Suppose you've got the following list (first line has field names, second
line has types and any row after is data):

csv = "name,age,place\nstring,int,string\nMac,25,Zurich\nMike,55,Oslo"

and you would like to turn it into a dictionary:

parsed = {
"name":["Mac", "Mike"],
"age":[25, 55],
"place":["Zurich", "Oslo"]
}

A trivial task in Python. In C++ it is cumbersome. I was thinking to put
the parsed data into a map:

map<string, vector<???> >

I have no problem with the key (string) but the value (vector) needs to be
of varying type. I think C++-STL does not allow what I want.

The following proposal is useless:

[...]
A simple solution, make two maps:

std::map<std::string, std::vector<std::string> > result_name;
std::map<std::string, std::vector<int> > result_age;
[...]

I want to build the map of lists dynamically - it can have many fields or
just one... and I would like to have simple interface (as Python offers).

- I cannot import python into the c++ environment (Borland Developer 2006)
and I know no one who was able to.

- I cannot write (*sigh*) the app in Python.

Any suggestions are very welcome!


Regards,
Marco (Forced to code in c++ again let me estimate the simplicity of
python)
 
F

Farshid Lashkari

Any suggestions are very welcome!

Are you allowed to use any boost libraries? If so then boost::any would
probably help. The boost::any object can contain any object type. So you
could have a single container that looked like the following:

std::map< std::string, std::vector<boost::any> > result;

Since you know the type, you would use boost::any_cast to convert it to
that type. You can find information about it here:

http://www.boost.org/doc/html/any.html

-Farshid
 
N

nnorwitz

Marco said:
This is actually a c++ problem. Got no satisfying answer on comp.lang.c++.
Maybe you can help better because I know, there are many c++-converts ;).

Suppose you've got the following list (first line has field names, second
line has types and any row after is data):

csv = "name,age,place\nstring,int,string\nMac,25,Zurich\nMike,55,Oslo"

and you would like to turn it into a dictionary:

parsed = {
"name":["Mac", "Mike"],
"age":[25, 55],
"place":["Zurich", "Oslo"]
}

A trivial task in Python. In C++ it is cumbersome. I was thinking to put
the parsed data into a map:

map<string, vector<???> >

I have no problem with the key (string) but the value (vector) needs to be
of varying type. I think C++-STL does not allow what I want.

This is so scary, I probably shouldn't post this. It sounds from your
description, you really want RTTI. I haven't done this in a long
while, so I'm not sure there is an easier way. But the program below
works and seems like it may be what you are asking for.

n
--
#include <stdio.h>
#include <vector>

struct Any {
virtual ~Any() { };
};

typedef std::vector<Any*> List;

class Str : public Any {
public:
Str(const char *s) : value_(s) {};
operator const char*() const {
return value_;
}
private:
const char *value_;
};

class Int : public Any {
public:
Int(int s) : value_(s) {};
operator int() const {
return value_;
}
private:
int value_;
};

int main(int argc, char **argv) {
Str anyone = "someone";
Int my_age = 5;
List list;
list.push_back(&anyone);
list.push_back(&my_age);

for (List::const_iterator i = list.begin(); i != list.end();
i++) {
const Int *iptr;
const Str *str;
if ((str = dynamic_cast<const Str*>(*i)))
printf("string: %s\n", (const char*) *str);
else if ((iptr = dynamic_cast<const Int*>(*i)))
printf("int: %d\n", (int) *iptr);
else
printf("clueless: %p\n", *i);
}

return 0;
}
 
M

Marco Aschwanden

This is so scary, I probably shouldn't post this. It sounds from your
description, you really want RTTI. I haven't done this in a long
while, so I'm not sure there is an easier way. But the program below
works and seems like it may be what you are asking for.

Perfect! That is precisely what I was looking for. I can easily adapt it
for my needs! Thanks a lot.

Regards,
Marco
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,291
Messages
2,571,453
Members
48,131
Latest member
KatlynC08

Latest Threads

Top