C++ - 多维数组 [英] C++ - Multidimensional Arrays

查看:50
本文介绍了C++ - 多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在处理多维数组时,是否可以为数组分配两种不同的变量类型...

When dealing with multidimensional arrays, is it possible to assign two different variable types to the array...

例如你有数组 int example[i][j] 是否有可能 ij 是两个完全不同的变量类型如 int 和 string?

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?

推荐答案

听起来您正在寻找:

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

或者也许:

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;

这篇关于C++ - 多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆