Jake Freelander
Jake Freelander

Reputation: 1471

Cant make a vector of fixed size arrays?

I have this odd problem

    vector<unsigned int[3]> tris;
    for (unsigned int i = 0; i < idx.size() - 2; i++) {
        unsigned int push[] = {idx[i], idx[i+1], idx[i+2]};
        tris.push_back(push); //<- this is where it goes belly up
    }

The code piece is supposed to unravel a triangle strip index list into triangle indices but wont compile under vs10. Thoughts?

Upvotes: 14

Views: 7197

Answers (3)

campisano
campisano

Reputation: 231

If you are using C++11 you can use tuples.

std::vector < std::tuple< unsigned int, unsigned int, unsigned int > > tris;

https://stackoverflow.com/a/15734156/846686

A less 'elegant' solution can be a pair of a pair

    std::vector < std::pair< unsigned int, std::pair<unsigned int, unsigned int> > tris;

but it may result in a very confused code to read...

Upvotes: 0

AnT stands with Russia
AnT stands with Russia

Reputation: 320381

No, unless you wrap your arrays into a struct or use something like std::array.

The naked C-style array type is not copyable or assignable, which makes it ineligible to serve as a standard container element.

P.S. It should be noted though that C++11 switched to a more elaborate (per-method) approach to specifying requirements to container element type, instead of the more broad approach used by C++03. The above ineligibility claim is based on C++03. I'm not ready to say that it is so unconditionally true for C++11 as well... But it is certainly true even in C++11 if you insist on using push_back in your code.

P.P.S. In C++11 one can probably get away with std::list of naked C-style arrays and using emplace to construct new elements. But not with std::vector.

Upvotes: 24

Juraj Blaho
Juraj Blaho

Reputation: 13451

Use std::vector<std::array<unsigned, 3>> instead.

Upvotes: 12

Related Questions