re: array pointing to another array - how to locate x in array[x]

Started by
4 comments, last by Aardvajk 17 years, 9 months ago
lets say I have an array of some objets - item[5]; then I have another array of where they can go - location[3] ( so only 3 items per location) when one of the item[#] is in the array,item[1] in location[0] for example - then location[0] will point to item[1] is there any way I can find out which item[#] is in this location with out storing the # in the item[] object? seeing as how 70% of the answers re: arrays are 'just use a vector duh' i would also love for everybody to tell me how much greater vectors and lists are because you can optimize the synchronization of memory configurations to maximize the optimal read/write synchronious operators -- and how nobody should ever use arrays and then get into a debate about it with everybody else!! yayyyy!!! yeah! I am so sarcastic! or maybe just the first question thanks@!!
Advertisement
If I understand you correctly, you could just make location[] and array of integers, where each int is your item index.

so:
Item items[5];
int locations[3];

locations[0] = 2; //items[2] is at location[0]

Edit: I know you dont want to use the STL (for some reason...) but this could also be done in a std::multimap (or whatever the map type is that lets you have duplicate keys), where your map holds <Item, int>. Then for every item you could hold its location as its key, and maybe have a value like -1 for not in a location.
I will checkout that multimap thing looks like it might work.


the first thing you suggested I can not do but thanks!

I have no problem using the STL, just trying to figure out a solution with out having to rewrite everything
I can sense what you're looking for is relatively simple, but I'm having difficulty figuring out exactly what you're saying. Could you show some code for what you've attempted, and comment what you'd like it to be able to do that it currently doesn't?
int i, k;fount = true;for (i=0; i<location_coint && !found; i++){  for (k=0; k<item_count && !found; k++)  {    if (locations[k] = item_to_search_for)    {      found = true;    }  }}if (found){  // item is found in location i in item k}
Wouldn't a better approach be (keeping it very simple)

struct Item{int Type;string Name; // etcint Location;};Item Items[100];int find_location(int ID){    for(int i=0;i<100;++i) if(Items.ID==ID) return Items.Location;}// ORint find_location(int Index){    assert(Index>=0 && Index<100); return Items[Index].Location;}


Then you don't need a location array at all in terms of storing where the items are, and you can have any number of items all at the same location.

Perhaps missing your point.

This topic is closed to new replies.

Advertisement