Jump to content
  • Advertisement
Sign in to follow this  
TheRealDeal

Array of Lists

This topic is 2805 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Well, I come here today for a bit of help getting this array of lists to sort itself out correctly. I can't seem to get it quite right on how it would work.

What I'm looking to do is have a single dimension array that holds in each index a list of my structs. In short, each index needs to be able to hold 0 or more structs and I would like to have access to each list by index, something like:

int main() {
list<position*> grid;
list<position>::iterator iter;
myStruct myStructVariable;

...

grid.resize(height*width);
for (i = 0; i < height*width; i++) {
iter = grid.begin();
grid.insert(iter, myStructVariable);
}

My current issue is that I'm getting a "no match for operator[] in grid" error every time I try to use a function from the list library such as insert. Assuming this all is the correct way to go about an array of lists, what I hope is that if I want, for example, all of the structs in the 3rd position, I would hopefully do something like

list<position> currentPositionsList = grid[2];

and then use an iterator to scan through all the structs in that list, and then have such a list for every index in the array. If it matters at all to make sense of the indexing, I'm using a single dimension to represent a coordinate, the above example being the same as (3, 0) using an upperleft corner origin.

I do get the feeling this is going to be something fairly simple to fix like a mistake with the pointer, but I'm having a devil of a time finding it. If more context needs to be provided (code or in terms of what I want) I'll post again in a few hours when I can come back to my laptop, but I think this should suffice. Any suggestions would be greatly appreciated.

Thanks!
JDDeal

Share this post


Link to post
Share on other sites
Advertisement
std::list doesn't have a bracket operator. lists don't store members in contiguous memory so such an operation would be O(n) at best. If you want to have quick random access you should be using a std::vector

-me

Share this post


Link to post
Share on other sites
Hi TheRealDeal.

The error you are getting is showing you that std::list has no operator []. If your grid is a fixed size (height*width) and you really need a random access operator [] consider using a container like std::vector or std::deque.

I'm generally confused by what you are trying to achieve here. Could you clarify?
If the container type is list<position*> grid; then you can only store position pointers in it, and you can't store your myStructs in it.

Which language are you using here? I was presuming you are using C++.

Share this post


Link to post
Share on other sites
Fancy that, my phone does work well enough with this site. I see what you mean now after looking at the documentation for vectors. I was assuming that since I could declare it with brackets I could access it the same way.

Syntax aside, if I more or less just change everything to vector instead of list, would I be able to accomplish what I described?

Share this post


Link to post
Share on other sites
Quote:
Original post by TheRealDeal
Syntax aside, if I more or less just change everything to vector instead of list, would I be able to accomplish what I described?


It's a little unclear what you actually intend to do, but given the code you posted, yes.

-me

Share this post


Link to post
Share on other sites
Steven, the pointer may have been a mistake, and if it truly makes no sense to have it then it can be considered as good as gone. Between your post and Palidines I do understand the issue with the list.

The clarify my goal, imagine a 5x5 grid. Each point on this grid can contain an arbitrary number of my structs, including 0. These can each change at any time. I would like a list of sorts that gives me access to this 0+ number of structs so I can change any one in that list as needed. I now need such a list for every grid location. I'm simply doing a single dimension list to represent each coordinate instead of a 2d one. Thus the idea of an array of lists, or rather vectors if that is a better choice for my needs.

Another way to think of it would be a multimap I believe. One key can have multiple values. And yes, c++ is the language.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!