From Python to c++

Gonzalo Monzón gmc at serveisw3.net
Wed Mar 22 01:41:05 CET 2006


Hi Marco!

Perhaps the simpler way could be to store them all in the map as string 
data, but of course, it depends on what you must do later with that 
data, so you can save the field types and convert from string when 
needed to use any value... if you do not need to process later that data 
at all, could be fine.

Otherwise if need a better approac, or if you need to store the values 
using the right types specified on the file data, you would make a 
simple template where to pass type when creation and inside that, chose 
the right container to use. I think you can find some examples on 
template creation googling a bit, and probably is the best approach for 
the case.

Sorry but my stl is a bit rusty as to put here a quick example, but be 
sure stl allows almost anything to be done.

Regards,
Gonzalo

Marco Aschwanden escribió:

>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)
>
>
>  
>




More information about the Python-list mailing list