-1

I was wondering how to do the following efficiently. Given brackets of integers, create a stl vector accordingly?

e.g. given

[[-9],[-8,0],[-3,2,5],[6,3,0,-4],[-2,-9,-5,-8,6],[0,-5,0,-2,-1,5],[0,6,-1,-5,-8,6,-5],[-8,-5,-9,-8,-4,-3,-5,7]] 

then, construct a 2d vector from it.

vector<vector <int> > vv;

EDIT:

  1. I do not have latest compiler with c++11, so the way that directly construct like,

    std::vector v({ 1, 2, 3, 4, 5 });

    is not what I want.

  2. what I currently have in mind is as follows.

    int x1[1]={-9};

    int x2[2]={-8, 0}; ...

    std::vector v1(x1, x1 + sizeof x1 / sizeof x1[0]);

    std::vector v2(x2, x2 + sizeof x2 / sizeof x2[0]); ...

    vv.push_back(v1);

    vv.push_back(v2); ...

But this is too tedious, and I have quite a lot of brackets.

TemplateRex
  • 69,038
  • 19
  • 164
  • 304
pepero
  • 7,095
  • 7
  • 41
  • 72

1 Answers1

1

Did you mean:

#include <vector>

int main()
{
  std::vector<std::vector<int>> vv = {{-9},{-8,0},{-3,2,5},{6,3,0,-4},{-2,-9,-5,-8,6},{0,-5,0,-2,-1,5},{0,6,-1,-5,-8,6,-5},{-8,-5,-9,-8,-4,-3,-5,7}}; 
}

Live example (gcc / clang in C++11 mode).

TemplateRex
  • 69,038
  • 19
  • 164
  • 304