visual studio 2008 - C++: How to create multi-type and multi-dimensional array format? -
i'm trying read similar array format php uses:
"data" => array( "keyname1" => "string", "somearraykeyname" => array( "keyname1" => "more strings", "keyname2" => "bla bla", "moar" => array( "first" => 1, "second" => 2, "third" => "3", ), "keyname3" => 25, ), "keyname2" => 13.37, "keyname3" => 1337, "array without keynames" => array( "keke", "lala", "lolo", ), );
but stumbled upon on multiple possible types problem. how can done in c++ best way ?
i thinking create struct possible types each element:
struct generaltype { char whichtype; // type using value int intval; float floatval; string stringval; };
and when read element, decide value type get, , convert value correct type , set correct variable (intval, floatval or stringval). keep track type value using whichtype variable, can pick correct variable when use value.
is anywhere near approach?
also, theres problem; how can define std::vector without knowing depth of array beforehand? type vector<vector<generaltype> >
if each element array itself. not elements arrays. so, dont know how solve std::vector. possible ?
first idea had, make key sequence string stored in std::map key, std::map value generaltype
type, , way able define dimensional easily, , value want quickly, this:
mymap["data/somearraykeyname/moar"] = generaltypevalue; // value not used, marker only, can first item position in std::map easily. mymap["data/somearraykeyname/moar/first"] = generaltypevalue; // array "moar" key "first" value.
but method prevents me looping through elements specific array. although, keys sorted, should have correct order always; but, require me parse key each time, , check if array changed @ next element. work, not efficiently.
what other ideas there this?
edit: want able call n-dimension array like: array["data"]["somearraykeyname"]["moar"]["first"]
, if value type array, able loop array if vector of generaltype
use boost::variant. remember type inside , pretty easy use.
Comments
Post a Comment