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
  • 10,356
  • 2
  • 35
  • 93

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
  • 22,024
  • 5
  • 133
  • 166
Vlad from Moscow
  • 265,791
  • 20
  • 170
  • 303
  • 1
    Formally, the type needs to be [*Erasable*](https://en.cppreference.com/w/cpp/named_req/Erasable) – Ayxan Haqverdili 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 – Ayxan Haqverdili Nov 28 '21 at 21:32