没有连续索引的Arry元素 [英] Arry elements with not consecutive indexes

查看:48
本文介绍了没有连续索引的Arry元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我想知道如果有任何方法可以为指针加倍分配两个内存位置,但这样的位置不会连续。


我真正想要的是拥有一个两个组件的向量,我可以得到索引为1的第一个组件,它意味着:VECTOR [1]但是要得到第二个组件我不要我不想使用索引二:VECTOR [2]。


我希望能够使用其他索引来获取它,例如10:VECTOR [10]


由于内存效率的原因,我不想为中间位置分配内存。


谢谢。

Hello,

I woul like to know If there is any way to allocate two positions of memory for a pointer to double but in such way that this positions are not consecutive.

What I really whant is to have one vector of two components where I can get the first component with the index 1, it means: VECTOR[1] but to get the second component I don''t want to use the index two: VECTOR[2].

I would like to be able to get it using other index, for instance 10: VECTOR[10]

I dont want to allocate memory for the positions in the middle because of reasons of memory efficiency.


Thank you.

推荐答案

我不认为有一种方法可以不使用两个不同的变量(而不是数组/向量)。数组实际上是指向许多的第一个元素的指针 - 每次使用[x]运算符时,您都在该指针之前访问内存x *(变量的大小)。因此,数组不能在很多位置分割。


但是,向量应该将这些元素存储在不同的位置,将指针保存到自己的数组中的每个位置(或者一些类似的实现)。但是,您仍然无法创建具有2个元素的Vector并访问vector [10]处的元素。但是,您可以创建一个具有10个元素空间的向量,但只能在指定的索引处添加2或3.
I don''t think there is a way to do this without using two different variables (instead of arrays/vectors). Arrays are actually pointers that point to the first element of many - each time you use the [x] operators, you are accessing the memory x * (size of variable) ahead of that pointer. Thus, an array cannot be split up over many locations.

A vector, however, should be storing these elements in separate locations, holding the pointers to each location in its own array (or some similar implementation). However, you still cannot create a Vector with 2 elements and access the element at vector[10]. You may, however, be able to create a vector with room for 10 elements, but only add 2 or 3 at specified indexes.


嗯,您可以定义自己的向量,就像重载的类一样运营商[]。你也可以使用地图。我不确定它的内存效率如何,但元素的位置可能是也可能不是并排(很可能不是),因为这是一个哈希值,根据维基百科通常实现为平衡树,这意味着对任何元素的访问是O(lg n),其中n是地图中元素的数量。 (lg = log base 2)


只需将第一个模板参数设为int(这是你的密钥)即可。看起来像一个数组的实际上是一个地图。


此外,当你有许多元素时,这比数组有更多的内存开销,但对于一个sparce数组,这是非常好的。 br />

以下是如何使用它的示例: http://www.sgi.com/tech/stl/Map.html


希望这会有所帮助。



Adrian
Hmm, you could define your own vector like class that overloads the operator[]. You could also use map. I''m not sure how memory efficent it is, but the locations of the elements may or may not be side by side (most likely not) as this is a hash and according to Wikipedia is usually implemented as a balanced tree, which means that access to any elment is O(lg n), where n is the number of elements in the map. (lg = log base 2)

Just make the first template parameter an int (which is your key) and away you go. What looks like an array is really a map.

Also, this has more memory overhead than an array when you have many elements, but for a sparce array, it is pretty good.

Here is an example of how to use it: http://www.sgi.com/tech/stl/Map.html

Hope this helps.


Adrian



我认为如果不使用两个不同的变量就有办法做到这一点(但是,您可以创建一个具有10个元素空间的向量,但只能在指定索引处添加2或3.
I don''t think there is a way to do this without using two different variables ( You may, however, be able to create a vector with room for 10 elements, but only add 2 or 3 at specified indexes.



这就是我现在所做的,但它在记忆方面效率不高。


无论如何,谢谢你

That is what I do now, But it is not efficient in terms of memory.

Thank you anyway


这篇关于没有连续索引的Arry元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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