1

I have an array which is to be filled using an object like this -

const std::map<Id, std::vector<Data>> *const DataSets[]=
{
    &object.data1,
    &object.data2,
    &object.data3,
    &object.data4
};

Condition here is, If object.data1.size() == 0 I dont want to push it into array. in that case I want to fill my array like this -

const std::map<Id, std::vector<Data>> *const DataSets[]=
{
    &object.data2,
    &object.data3,
    &object.data4
};

UPDATE I am using std::vector instead of array now and trying to initialize vector in same as array -

const std::vector<std::map<Id, std::vector<Data>>> *const DataSets
{
    &object.data1,
    &object.data2,
    &object.data3,
    &object.data4
};

I am getting error: E0146 too many initializer values. Can't I initialize my vector in this way? If not can anyone please suggest how to do that?

Thanks in advance!

8
  • 2
    Why not use std::vector instead, and push the items as needed? Commented May 18, 2018 at 6:33
  • thanks for reply @Some programmer dude since my further logic depends upon this arraya and the code is long back implemented...thatswhy not using vector. Commented May 18, 2018 at 6:36
  • Then there is simply no way to solve your problem. With a vector, and initializing it as soon as you have all the objects needed in in it, that's the only reasonable way to solve it in C++. If you do this in the global (or namespace) scope then another warning: Unless the objects are all defined in the same translation unit the pointers you have might not even be valid. Commented May 18, 2018 at 6:39
  • how it could be implemented if you do not know size.. also reading from vector is done in some way as from array... Commented May 18, 2018 at 6:39
  • 1
    Also remember that when you pass an array to a function, it naturally decays to a pointer to its first element. And you can get such a pointer from a vector as well, if you need backward compatibility. Commented May 18, 2018 at 6:43

2 Answers 2

1

You don't do that.

Respectively you don't use C style plain arrays if you want to do anything dynamic. You just wrap it in yet another std::vector because that supports dynamic sizes.

Sign up to request clarification or add additional context in comments.

3 Comments

next step in my code I have a function which takes this array as one of the parameter. I cant change previous implementation but need to send data as an array...
std::vector::data() and std::vector::size() are suitable for interfacing with that legacy interface.
hi @Ext3h can you please suggest solution for above? I have updated the question.
1

[...] since my further logic depends upon this arraya and the code is long back implemented...thatswhy not using vector

Thats not a good reason for not using a vector. If you ever need a c-array you can still use std::vector::data() in combination with std::vector::size(). There is (almost) no good reason to prefer a c-array to a std::vector, even if you need c-arrays in some places.

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.