nested lists

nested lists

Here is where ideas can be collected for the skirmish AI in development

Moderators: hoijui, Moderators

Post Reply
User avatar
krogothe
AI Developer
Posts: 1050
Joined: 14 Nov 2005, 17:07

nested lists

Post by krogothe »

Say i want to make nested lists but im nesting arrays of lists like these:

Code: Select all

	ground_factories = new list<int>[numOfSides];
	sea_factories = new list<int>[numOfSides];
	ground_defenses = new list<int>[numOfSides];
The above were all initialized, declared etc properly and work. Now i want to stick those lists in one bigger list (having each list, eg ground_factories as an element)

I looked up in the internet and found nothing related to this :( so i tried things like:

Code: Select all

	list<list<int>>	*all_lists;
	all_lists = new list<list<int>>;
	all_lists.push_back(ground_factories);
I get an error:

Code: Select all

.\UnitTable.cpp(37) : error C2228: left of '.push_back' must have class/struct/union
        type is 'std::list<_Ty> *'
        with
        [
            _Ty=std::list<int>
        ]
        did you intend to use '->' instead?
How would i implement this?
SoftNum
Posts: 22
Joined: 28 Nov 2005, 22:07

Post by SoftNum »

Try

Code: Select all

all_lists->push_back(ground_factories); 
But, I would make all_lists not a pointer.
User avatar
mr sharpoblunto
Posts: 24
Joined: 12 Dec 2005, 03:47

Post by mr sharpoblunto »

This code should be what your after.

Code: Select all

ground_factories = new list<int>(5);
sea_factories = new list<int>(5);
ground_defenses = new list<int>(5); 
   
list<list<int> *>   all_lists;//stores pointers to the nested lists

all_lists.push_back(ground_factories);
all_lists.push_back(sea_factories);
all_lists.push_back(ground_defenses);

also be sure to free up the memory assigned to these lists when your finished to prevent memory leaks

Code: Select all

     for (list<list<int> *>::iterator iter=all_lists.begin();iter!=all_lists.end();++iter)
    	delete (*iter);
User avatar
krogothe
AI Developer
Posts: 1050
Joined: 14 Nov 2005, 17:07

Post by krogothe »

mr sharpoblunto, thank you! that works just fine!
man its sooo good to have so many pro c++ programmers to help me!
ill go off to make pretty trees now!
User avatar
jcnossen
Former Engine Dev
Posts: 2440
Joined: 05 Jun 2005, 19:13

Post by jcnossen »

Storing pointers to the list is not really necessary in this case though, then you don't have to delete them after:

Code: Select all

list<list<int> > all_lists;

all_lists.push_back(list<int>());
list<int> *ground_factories=&all_lists.back();

all_lists.push_back(list<int>());
list<int> *sea_factories=&all_lists.back();

all_lists.push_back(list<int>());
list<int> *ground_defenses=&all_lists.back();
User avatar
krogothe
AI Developer
Posts: 1050
Joined: 14 Nov 2005, 17:07

Post by krogothe »

any advantages in speed over either method?
User avatar
jcnossen
Former Engine Dev
Posts: 2440
Joined: 05 Jun 2005, 19:13

Post by jcnossen »

if any difference, the last method is faster mainly because you're calling the memory manager less often.
Post Reply

Return to “AI”