简体   繁体   English

在C ++中初始化2D向量

[英]initialising 2d vectors in c++

I'm trying to create a 2d vector in c++ I declare it using: 我正在尝试在c ++中创建2d向量,我使用以下方法对其进行了声明:

std::vector < std::vector <TYPE> > VARIABLE;

which works fine. 效果很好。 Is it possible to declare the 'length' of the first vector whilst declaring the variable without declaring the other? 是否可以在声明变量时声明第一个向量的“长度”而不声明另一个变量? ie can I say Variable = a vector of length 2, each of which is a vector of some unknown length? 即我可以说Variable =一个长度为2的向量,每个向量都是某个未知长度的向量吗?

Is it possible to declare the 'length' of the first vector whilst declaring the variable without declaring the othe? 是否可以在声明变量时声明第一个向量的“长度”而不声明其他变量?

Kind of, assuming that by "first vector" you mean the outer one. 有点,假设“第一个向量”是指外部的。 You can do this: 你可以这样做:

std::vector < std::vector <TYPE> > VARIABLE(2);

so VARIABLE will consist of two empty std::vectors<TYPE> . 因此VARIABLE将由两个空的std::vectors<TYPE> There is no such thing as "unknown length" in this context. 在这种情况下,没有“未知长度”这样的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM