-1

How to define a vector that will contain two-dimensional arrays.

eg: vector<array[16][16]> vec; // where the two dimensional array have string elements

which will contain vec ={array_1[16][16],array_2[16][16],array_3[16][16],....};


I have tried:

vector<array<array<string,16>,16>> vec;

but it does not work when I use vec.push_back(array);

error shown is:

No matching member function for call to 'push_back'

minimal code:

string arr[16][16];

vector<array<array<string,16>,16>> vec;

vec.push_back(arr);
Adithya Shetty
  • 1,247
  • 16
  • 30

1 Answers1

2

You shouldn't name a variable like a type. You are shadowing std::array. You should rename the variable and use std::array instead of C-arrays:

#include <array>
#include <string>
#include <vector>

using StrArr2D = std::array<std::array<std::string, 16>, 16>;

int main() {
    StrArr2D arr;
    std::vector<StrArr2D> vec;
    vec.push_back(arr);
}

You can keep the variable name array if you avoid using namespace std; and use full qualified names.

Thomas Sablik
  • 16,127
  • 7
  • 34
  • 62
  • i am getting a as error `Implicit instantiation of undefined template 'std::array, std::allocator>, 16>, 16>'` when I tried the above – Adithya Shetty Jan 27 '21 at 17:26
  • 1
    Why not just use `arr` as the variable name? Like you use `vec` for the `vector` variable. Then you don't have to worry about the `using namespace std` issue. – Remy Lebeau Jan 27 '21 at 17:41
  • @RemyLebeau The variable name was `array` earlier. I wanted to show different ways. But now, after OP changed the code in the question, it makes no sense to call the array `array`. – Thomas Sablik Jan 27 '21 at 17:43
  • 1
    @ThomasSablik In your example, I would add a `using` declaration to make the code earlier to read, eg: `using StrArr2D = std::array, 16>; StrArr2D arr; std::vector vec; ...` – Remy Lebeau Jan 27 '21 at 17:49