Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
217 views
in Technique[技术] by (71.8m points)

c++ - How to define a vector of two dimensional array?

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);
question from:https://stackoverflow.com/questions/65923989/how-to-define-a-vector-of-two-dimensional-array

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...