2

I want to keep a vector of numbers. I'm not sure what having a vector of arrays looks like, but I'd imagine it looks like this:

std::vector<int[5]> myvector;

And then access it like this:

myvector[3][4] = 3;

Strangely (or not) I've never seen this technique before. Is there a reason for it. Is it just inherently uncommon for such a thing? Or would it be better to wrap the array in a class object type?

Zebrafish
  • 11,682
  • 3
  • 43
  • 119

1 Answers1

7

In general you can not create a vector of arrays because arrays do not have the assignment operator and the copy constructor.

But you can create a vector of objects of the type std::array<int, 5>

Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 1
    Formally, the type needs to be [*Erasable*](https://en.cppreference.com/w/cpp/named_req/Erasable) – Aykhan Hagverdili Nov 28 '21 at 21:31
  • but many member functions impose stricter requirement https://en.cppreference.com/w/cpp/container/vector#:~:text=requirements%20of%20Erasable%2C-,but%20many%20member%20functions%20impose%20stricter%20requirements.,-This%20container%20(but – Aykhan Hagverdili Nov 28 '21 at 21:32