0

I am trying to convert Perl code into c++ but facing problem to convert hash variable. map is the option to convert but it wont work for some cases.

my %xyz =( "x1" => {"z"=>1,
                "y"=>0,
                "a"=>2})

in c++

map<string,map<string, int> > xyz;

and we can use insert or emplace to store values in xyz but for below declaration, how we can use map or any other option available in c/c++

my %xyz = ("x1" => {"z" => 1,
                    "y"=> 0,
                    "a"=> {"y1"=>0 }},
           "x2" => {"Version"=> "x.300 x.400 x.500   x.600"})

thanks in advance

erdarshp
  • 423
  • 1
  • 5
  • 12
  • Create a class that can either have a single integer value or be a map of strings to instances of itself. You could probably also use almost any JSON library -- they usually provide just such a discriminated value class. – David Schwartz Mar 06 '15 at 06:39

2 Answers2

0

Use a JSON library such as jsoncpp. The following JSON object encodes the same information as your example:

{
    "x1" :
    {
         "z" : 1,
         "y" : 0,
         "a" :
         {
             "y1" : 0
         }
    }
    "x2" :
    {
        "Version" : "x.300 x.400 x.500   x.600"
    }
}
David Schwartz
  • 179,497
  • 17
  • 214
  • 278
0

I can think of instead of using the std::map, you can store xyz variable as an xml format string or a json format string. XML format will look like as shown below.

<xyz>
  <x1>
    <z>1</z>
    <y>0</y>
    <a>
      <y1>0</y1>
    </a>
  </x1>
  <x2>
    <Version>x.300 x.400 x.500  x.600 </Version>
  </x2>
</xyz>

Then you can use the xml parser. What XML parser should I use in C++?

Community
  • 1
  • 1
kvivek
  • 3,321
  • 1
  • 15
  • 17