简体   繁体   English

C ++ - 多维数组

[英]C++ - Multidimensional Arrays

When dealing with multidimensional arrays, is it possible to assign two different variable types to the array... 处理多维数组时,是否可以为数组分配两种不同的变量类型...

For example you have the array int example[i][j] is it possible for i and j to be two completely different variable types such as int and string? 例如,你有数组int example[i][j] ij有可能是两个完全不同的变量类型,如int和string吗?

Sounds like you're looking for: 听起来像你在寻找:

std::vector<std::map<std::string, int> > myData1;

or perhaps: 也许:

std::map<int, std::map<std::string, int> > myData2;

The first would require you to resize the vector to an appropriate size before using the indexing operators: 第一个要求您在使用索引运算符之前将向量调整为适当的大小:

myData1.resize(100);
myData1[25]["hello"] = 7;

...while the second would allow you to assign to any element directly (and sparsely): ...而第二个允许你直接(和稀疏)分配给任何元素:

myData2[25]["hello"] = 7;

No. That's not possible. 不,那是不可能的。 You may want to look into using the STL map . 您可能希望查看使用STL映射

No, C++ only allows integer types (ex: int, long, unsigned int, size_t, char) as indexes. 不,C ++只允许整数类型(例如:int,long,unsigned int,size_t,char)作为索引。

If you want to index by a string, you could try std::map<std::string,mytype> but it gets complicated trying to extend that to two dimensions. 如果你想用字符串索引,你可以尝试std::map<std::string,mytype>但是尝试将它扩展到二维时会变std::map<std::string,mytype>复杂。

不,但你可以使用std :: maps

不,您只能使用整数类型作为索引。

No you can't. 不,你不能。 You could achieve this with std::map though. 你可以用std::map来实现这个目的。

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

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