Archived

This topic is now archived and is closed to further replies.

STL - list<queue<int> >

This topic is 5978 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 for the timeline in my game i need a list of queues of a selfwriten class. To make the example less complex i use a list of queues of ints:
      
#pragma warning(disable:4786) 

#include <list>

#include <iostream>

#include <queue>

using namespace std ;


int main()
{
    list<queue<int> > my_timeline:
}  
Well my question is how do i use this thing? Lets say i want: to put the numbers 2,3,5 in the first knot of the list an empty que in the second list knot. What must i write? Please help. Edited by - Jonus on August 2, 2001 1:39:04 PM

Share this post


Link to post
Share on other sites
Well, it gets a little messy, honestly. Since your list is holding a complicated object (as opposed to a simple type), you''ll need to put instances of that type in the list.

For example:

  
queue<int> temp; // when we first create this, it''s empty...


// ...so we can create the first "knot"

temp.push_back(2);
temp.push_back(3);
temp.push_back(5);

// put it in the list

my_timeline.push_back(temp);

// now want an empty list for the next "knot" (we always have to clear it, of course, unless we want 2, 3, and 5 hanging around)

temp.clear();

// put it in the list

my_timeline.push_back(temp);


Now, if you want to modify queues already in the list, you''ll have to use an iterator to obtain a pointer to the particular queue you''ll want, then operate on it through that pointer.

Share this post


Link to post
Share on other sites
Thank you merlin9x9. That was exactly the information i needed (create temp instance and use it to create the knots).

I know how to use the containers, but i haven''t used a concainer for a container yet. Again thx.

Share this post


Link to post
Share on other sites
Your realization that there needs to be a space between the last two greater-than signs makes you better than I...it took me a long time to figure out that omitting the space made the compiler think I had a right-shift in there.

Share this post


Link to post
Share on other sites
I also believe you can use the [] operator to access the elements like in a 2d array. I''m not sure if you need ()''s but I use them just in case.

example:
(my_timeline[1])[2]
This line would access the first element in the list(the first queue) and the 2nd element in the queue (some number)


Later,
Eck

Share this post


Link to post
Share on other sites
Sadly, list has no operator[]. This is because list is almost always implemented as a linked list—if not, it behaves like one. As such, how would you get an index value for nodes that are potentially scattered all over the place? There are ways, of course, but most of them essentially offset the advantages of using something like list rather than vector in the first place.

Since I''m talking about vector now, maybe you can use vector instead so you can use indexing rather than messy iterators. If there''s some way you can know ahead of time how many items you''re going to store, use vector''s reserve function (passing it the number of items you''re going to add) to make it allocate the space ahead of time so it won''t reallocate for each addition. And once you''ve done that, the benefit is that all kinds of access are cheap; in contrast, list is slow for all but linear access.

Share this post


Link to post
Share on other sites
I am trying to rebuild the Active Time System of Final Fantasy Tactics.

FFT is not based on which Faction is at turn but which Character
is at turn. Every Character has its own Speed Attribute (e.g. Thief has speed 6, a heavy Knight 4).

Its easy to determine which character is at turn (not really), but when you try to predict, which will be the next twenty Chars at turn, it gets really complicated (plus there are spells which take influence on the flow of time).

IMHO this Problem can only be solved with a list of queues of player_on_map. (:

Share this post


Link to post
Share on other sites