我正在寻找可以在单个位置存储多个值的任何类型的编程数据结构。
例如
假设一个数组,其中每个索引都有多个值:
例如,如果数组大小为 5,那么在索引 0 处我们有 :1,2,3;
在索引 1 处,我们有: 4,5;
在索引 2 处:1、8、9、4;
等等...
除了链表之外,是否还有其他数据结构可以做到这一点。
我正在寻找的编程语言:JAVA/C/C++/lisp
您必须使用动态容器的数组,例如向量或列表。这是一个C++
使用的实现Array of vectors
:-
using namespace std;
#include <iostream>
#include <vector>
#define ARRAY_SIZE 5
int main ()
{
vector<int> array[ARRAY_SIZE]; //An array (5 element) of vectors
array[0].push_back(1);
array[0].push_back(2);
array[0].push_back(3);
array[1].push_back(4);
array[1].push_back(5);
array[2].push_back(1);
array[2].push_back(8);
array[2].push_back(9);
array[2].push_back(4);
for(int i=0; i<ARRAY_SIZE; i++)
{
cout<<"Contents at array index: "<< i <<":";
for(vector<int>::iterator it = array[i].begin(); it != array[i].end(); ++it)
{
cout << ' ' << *it;
}
cout << "\n";
}
return 0;
}
Output:-
Contents at array index: 0: 1 2 3
Contents at array index: 1: 4 5
Contents at array index: 2: 1 8 9 4
Contents at array index: 3:
Contents at array index: 4:
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句