tags:

views:

176

answers:

5

im storing some settings for objects in an array. the id's of objects are used as the key. the id's start from 100000 and go up. if i was to input data for an object with id 100 000, would cpp automatical create 99999 blank key entries starting from 0?

A: 

There may be a better container than a flat array. Choosing the right data structure depends on what you are trying to do. If you are storing objects using a key, you might want to use a std::map<key, value>.

What happens depends entirely on the data structure you choose to use. If you use a map, only the items you insert will take up space in memory. If you use new to allocate an actual array, then you will want to allocate only enough space for for the items you want to store. In that case, adjust your index by subtracting 100,000.

mch
+4  A: 

Array size is determined when you create an array.

To access object at index 100 000 you need to have array of at least that size, which answers your question.

If the array is smaller you will access memory at

array begin address + (index*object size)

which is not a good thing. E.g. the following will print some data but it is a data that are stored at that point in memory and it's outside of your array (not a good thing):

string arr[3];
cout << arr[5] << endl;

Assuming you are talking about standard array like:

string arr[10];

Array's size is specified when you compile it, for example you can't do:

string arr[]; // this will fail to compile, no size specified

But you do:

string arr[] = {"1","2","3"};  // array size is 3
string arr1[3];                // array size is 3
string arr2[3] = {"1"};        // array size is 3

If you want to map extra parameters for object you are better off using std::map like:

class person {};
std::map<person*,int> PersonScore;

This assumes that the additional parameters are not logically part of the object otherwise you would just add them to the object.

stefanB
is the `std::map<>` example backwards? (<V,K> instead of <K,V>)
D.Shawley
since the question was about storing additional parameters for objects I assume the object address is the key ... of course it would be much clearer if it used something like person.id as key to extract the int value ...
stefanB
map sounds like a good idea, but what id i wanted to associate more details with the map'ed keys...for example in a multidimensional array i could have int arr[2][3];int arr[id_of_object1][detail1] = value1;int arr[id_of_object1][detail2] = value2;int arr[id_of_object1][detail3] = value3;int arr[id_of_object2][detail1] = value1;int arr[id_of_object2][detail2] = value2;int arr[id_of_object2][detail3] = value3;where as with maps i only have one value i can assign?map<int, int> data;data["Ten"] = 10;data[id_of_object1] = value1;data[id_of_object2] = value1;sorry.new to this
Prodigga
sorry about the messy comment. didnt realize they come out that way
Prodigga
If I'm needing a map inside a map inside a map etc. (non-integer or non-sequential keys) I use typedefs to make it cleaner. However, std::map let's you do: std::map<int, std::map<int, std::map<int, person*> > > stuff;It's not a "best" method, and std::map's require more overhead (attempting to read from a key that doesn't exist is not an error, it will return an object at that key using the default constructor), but it's good for the common key/value stores.
Matt
+2  A: 

Maybe you want somthing along the lines of:

class ArrayPlus100k {
  Item underlyingArray[NUM_ELEMENTS];
public:
  Item& operator [] (int i) { return underlyingArray[i-100000]; }
  // etc.
}
Grumdrig
+1  A: 

If you truely mean an array, and by key you mean index, then subtracting 100,000 from your index will provide you with a zero based array index. There will be no unused entries.

James Wald
A: 

No, it will not create 0-99999. it will start from 100000 to ur array size. For example. If u declare

int arr[5]; if u start from arr[2] u can store upto arr[7];

I hope u understood...