3

I need to create a vector where each value is in fact four values. So good old Google led me to these pages:

Vector of a Fixed size array

Cant make a vector of fixed size arrays?

Vector of vectors, reserve

From these threads I'm led to believe it is optimal with the following syntax:

std::vector<std::array<unsigned int, 4>> arrayVector;

That part seems to work, but now I'd like to add my first row of values:

arrayVector.push_back({ 0, 1, 2, 3 });

And that part does not compile:

Severity Code Description Project File Line Suppression State Error (active) no instance of overloaded function "std::vector<_Ty, _Alloc>::push_back [with _Ty=std::array, _Alloc=std::allocator>]" matches the argument list

I've also tried creating the int array as a separate variable and passing that variable to the push_back, also with the same result.

I'm using Microsoft Visual Studio Community 2015, I'm not one to normally choose C++, so I might be missing something obvious. Thanks in advance.

Community
  • 1
  • 1
braks
  • 1,505
  • 15
  • 23
  • 1
    Hmm, it seems to compile for me [on IdeOne](https://ideone.com/kogZIJ)... – Ken Y-N Jan 24 '17 at 04:48
  • 1
    Your code gave me a big clue that nobody mentioned in those other threads: #include :D That could be it. I didn't think to check that because there was no compilation error for the first line where the std::array is used. – braks Jan 24 '17 at 05:08

2 Answers2

4

Try this:

arrayVector.push_back({{ 0, 1, 2, 3 }});

Older versions of Clang (for example) require the extra braces, for example--the reason seems to be that you're initializing the C-style array inside the std::array.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
1

Try


    arrayVector.push_back(std::array&ltunsigned int, 4&gt{0, 1, 2, 3});

london-deveoper
  • 533
  • 3
  • 8
  • That gives two errors: "incomplete type is not allowed" and "too many initializer values" – braks Jan 24 '17 at 05:04
  • 1
    it compiles for me using g++ 5.4. p.s. i just saw your comment about the header file. i think everyone assumed that it had been included (behind the scenes so to speak). cheers. – london-deveoper Jan 24 '17 at 05:12