C++ - 多维数组
处理多维数组时,是否可以为数组分配两个不同的变量类型...
例如,您有数组 int example[i][j]
是否可以为 i
和 j
是两种完全不同的变量类型,例如 int 和 string?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
听起来像您正在寻找:
或者也许:
第一个需要您在使用索引运算符之前将向量调整为适当的大小:
...而第二个将允许您直接(并且稀疏地)分配给任何元素:
Sounds like you're looking for:
or perhaps:
The first would require you to resize the vector to an appropriate size before using the indexing operators:
...while the second would allow you to assign to any element directly (and sparsely):
不,那是不可能的。您可能想考虑使用 STL 地图。
No. That's not possible. You may want to look into using the STL map.
不可以,C++ 只允许整数类型(例如:int、long、unsigned int、size_t、char)作为索引。
如果您想按字符串进行索引,可以尝试
std::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.不,但您可以使用 std::maps。
No, but you could use std::maps.
不可以,您只能使用整数类型作为索引。
No, you can only use integer types as indices.
不,你不能。不过,您可以使用
std::map
来实现这一点。No you can't. You could achieve this with
std::map
though.