Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 从二维数组中检索数组元素_C++ - Fatal编程技术网

C++ 从二维数组中检索数组元素

C++ 从二维数组中检索数组元素,c++,C++,我想在2D数组中存储256*256个条目。我是这样做的: struct Data { int Serial_Number; int NextIndex; } ; struct Data Index[256][256]; Data obj1; obj1.Serial_Number=1; obj1.NextIndex=5; Index[3][240]=obj1; 现在,一旦我存储了所有这些值,那么我将如何从数组元素中检索每个值 我想检索一个存储在索引[3][240]中的值

我想在2D数组中存储256*256个条目。我是这样做的:

struct Data
{
    int Serial_Number;
    int NextIndex;
} ;

struct Data Index[256][256];


Data obj1;
obj1.Serial_Number=1;
obj1.NextIndex=5;

Index[3][240]=obj1;
现在,一旦我存储了所有这些值,那么我将如何从数组元素中检索每个值

我想检索一个存储在索引[3][240]中的值

第二,这种方法比无序图更快吗

struct Data
{
    int Serial_Number;
    int NextIndex;
} ;

struct Data Index[256][256]; //should be this

Data obj1;
obj1.Serial_Number=1;
obj1.NextIndex=5;

Index[3][240]=obj1;
检索数据:
struct Data Data=Index[3][240]

访问结构的数据:
data.Serial\u编号

如果要逐个检索每个值,只需使用索引[0][0]到索引[256-1][256-1]之间的索引即可。但是,如果要从数组中访问特定元素,则需要循环数组以找到它

for(int i = 0; i < 256; ++i)
    for(int j = 0; j < 256; ++j)
        if(Index[i][j] == what you are looking for)
        {
            found!
        }
for(int i=0;i<256;++i)
对于(int j=0;j<256;++j)
如果(索引[i][j]==您正在寻找的内容)
{
建立
}

这个函数的时间复杂度是O(m*n)(m是数组的长度,n是数组的宽度),而
unordered_map::find()
在最好的情况下运行在O(1)中,在最坏的情况下运行在O(n)中(n是元素数,当哈希函数不好导致太多哈希冲突时,可能会发生这种情况,因为
无序映射
在其实现中使用哈希表)。

代码的最后一行
索引[3][240]的含义是什么=obj1;
???我的意思是,您是否试图在int
结构日期索引[256][256]中存储结构对象
?我犯了一个错误,我必须创建一个结构数据数组。我应该选择哪一个,因为我希望搜索/检索元素的速度更快?尽管
无序映射
在大O表示法方面优于数组,但很难判断哪一个更快。数组中的元素位于连续的内存区域中,因此如果您经常访问阵列,则会发生缓存未命中。您最好在计算机上测试它。