braks
braks

Reputation: 1610

How to add a value to a vector of arrays?

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.

Upvotes: 3

Views: 242

Answers (2)

london-deveoper
london-deveoper

Reputation: 543

Try


    arrayVector.push_back(std::array<unsigned int, 4>{0, 1, 2, 3});

Upvotes: 1

John Zwinck
John Zwinck

Reputation: 249293

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.

Upvotes: 4

Related Questions