Sign in to follow this  
polpoint

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

Recommended Posts

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@!!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites

int i, k;
fount = true;
for (i=0; i<location_coint && !found; i++)
{
for (k=0; k<item_count && !found; k++)
{
if (locations[i][k] = item_to_search_for)
{
found = true;
}
}
}
if (found)
{
// item is found in location i in item k
}

Share this post


Link to post
Share on other sites
Wouldn't a better approach be (keeping it very simple)


struct Item
{
int Type;
string Name; // etc

int Location;
};

Item Items[100];

int find_location(int ID)
{
for(int i=0;i<100;++i) if(Items[i].ID==ID) return Items[i].Location;
}

// OR

int 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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this