开发者

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 i开发者_如何学编程nt example[i][j] is it possible for i and j to be two completely different variable types such as int and 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.


No, C++ only allows integer types (ex: int, long, unsigned int, size_t, char) as indexes.

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.


No, but you could use std::maps.


No, you can only use integer types as indices.


No you can't. You could achieve this with std::map though.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜