user2837329
user2837329

Reputation: 83

Porting a big array from Python to C++ (Any Ideas)

So I'm porting a program from Python to c++ and I have this block of code here:

opcodes = [
        [0x1,'0x1',['b','b',]],
        [0x2,'call',['d',]],
        [0x3,'0x3',['w',]],
        [0x4,'0x4-return',[]],
        [0x5,'0x5',[]],
        [0x6,'0x6-condjump',['d']],
        [0x7,'0x7-condjump',['d']],
        [0x8,'0x8',[]],
        [0x9,'0x9',[]],
        [0xa,'0xa',['d',]],
        [0xb,'0xb',['w',]],
        [0xc,'0xc',['b',]],
        [0xe,'string',['str']],
        [0xf,'0xf',['w',]],
        [0x10,'0x10',['b',]],
        [0x11,'0x11',['w',]],
        [0x12,'0x12',['b',]],
        [0x14,'0x14',[]],
        [0x15,'0x15',['w',]],
        [0x16,'0x16',['b',]],
        [0x17,'0x17',['w',]],
        [0x18,'0x18',['b']],
        [0x19,'0x19',[]],
        [0x1a,'0x1a',[]],
      ]

I was wondering what would be the best way to convert this to a C++ array. I'm not too familiar with python sorry and heard this is called a nested list?

Thanks in advance for all answers, this is probably the biggest hurdle of the python code.

Upvotes: 1

Views: 97

Answers (2)

John Zwinck
John Zwinck

Reputation: 249203

This is C++11:

#include <string>
#include <vector>

struct OpCode {
  int code;
  const char* str;
  std::vector<const char*> extras;
};

OpCode opcodes[] = {
  {0x1,"0x1",{"b","b",}},
  {0x2,"call",{"d",}},
  {0x3,"0x3",{"w",}},
  {0x4,"0x4-return",{}},
  {0x5,"0x5",{}},
  {0x6,"0x6-condjump",{"d"}},
  {0x7,"0x7-condjump",{"d"}},
  {0x8,"0x8",{}},
  {0x9,"0x9",{}},
  {0xa,"0xa",{"d",}},
  {0xb,"0xb",{"w",}},
  {0xc,"0xc",{"b",}},
  {0xe,"string",{"str"}},
  {0xf,"0xf",{"w",}},
  {0x10,"0x10",{"b",}},
  {0x11,"0x11",{"w",}},
  {0x12,"0x12",{"b",}},
  {0x14,"0x14",{}},
  {0x15,"0x15",{"w",}},
  {0x16,"0x16",{"b",}},
  {0x17,"0x17",{"w",}},
  {0x18,"0x18",{"b"}},
  {0x19,"0x19",{}},
  {0x1a,"0x1a",{}},
};

Upvotes: 2

Amadan
Amadan

Reputation: 198324

Make a struct or a class that will hold [0x1,'0x1',['b','b',]], something like

struct shmizzle {
   int forpult;
   char *yorgole;
   char **flubbo;
};

Probably easier with a class, since you get to initialise it more easily. I'm not a C++ expert though.

Upvotes: 2

Related Questions