0

As we all know, it's normal to initialize an array of int like this:

int intAry[] = {7, 8, 9};

So, I want to know, how can initialize an array of std::vector in the same way(just in the initial list):

typedef std::vector<int> type;
type vecAry[] = {vec1, vec2, vec3};

I know it's legal to write code as fllows, now my question how to initialize vecAry in one line of code:

type vec1;
type vec2;
type vec3;
type vecAry = {vec1, vec2, vec3};
7
  • 2
    And why would you need array of vector ? Commented Jul 25, 2013 at 10:46
  • @0x499602D2, are you kidding me? you can't compile in C++. Commented Jul 25, 2013 at 10:46
  • @Hitesh Vaghani, why ask my requirement? I want to implement design pattern in my code. Commented Jul 25, 2013 at 10:50
  • 1
    Because vector of vectors is always there. Commented Jul 25, 2013 at 10:59
  • in your post should be type vecAry[] = {vec1, vec2, vec3}; ? Commented Jul 25, 2013 at 11:16

1 Answer 1

4

In C++11,

type vecAry[] {{},{},{}};

or if you want non-empty vectors

type vecAry[] {{1,2,3},{4,5,6},{7,8,9}};

If you're stuck in the past, you can initialise it with empty vectors:

type vecAry[] = {type(), type(), type()};

but you can't initialise it with arbitrary values without some kind of helper like those in the Boost Assignment library:

type vecAry[] = {list_of(1)(2)(3), list_of(4)(5)(6), list_of(7)(8)(9)};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.