-
list container type
This is my code:
Code:
vector <zone *> zone_route;
list<zone *>::iterator iter3=find(route.begin(),route.end(),st);
if (steps)
{
for(int i=0; i<=steps && i>=(-1*steps); i+=up_flag)
{
zone_route.push_back((*iter3));
switch (up_flag)
{
case -1:
iter3--;
break;
case 1:
iter3++;
break;
}
}
}
route is a list of pointers to zones and the aim is to iterate up or down that list, depending on the positive or negative nature of up_flag, copying the pointer each time into the vector zone_route. st at the end of the find() function is a pointer to the zone from which the search is starting. Steps is equal to 1. The problem is that it copies the first pointer fine, but then on the 2nd run through the loop, nothing is entered into the zone_route vector.
Can anyone see what I am doing wrong? I am new to the use of STL containers so there may be a problem there.
-
-
Ok, i sorted the problem, thanks Kuphryn.
I need to gain a bit more understanding though and have another question.
Here's some code:
Code:
zone* start_zone = &GetZoneRef(2);
list<zone *>::iterator iter = find(route.begin(),route.end() ,start_zone);
Here is GetZoneRef():
Code:
zone GetZoneRef(int ref)
{
if (zone_main_corridor.GetReference() == ref)
{
return zone_main_corridor;
}
if (zone_adjoining_corridor_lstairs.GetReference() == ref)
{
return zone_adjoining_corridor_lstairs;
}
if (zone_adjoining_corridor_rstairs.GetReference() == ref)
{
return zone_adjoining_corridor_rstairs;
}
if (zone_stairs.GetReference() == ref)
{
return zone_stairs;
}
if (zone_offices_corridor.GetReference() == ref)
{
return zone_offices_corridor;
}
}
The GetReference function is simply a member function to access the integer reference number of the zone.
My question is simply if I am passing in the correct argument at the end of the find() function? It is not working correctly. However if I do:
Code:
list<zone *>::iterator iter = find(route.begin(),route.end() ,&zone_stairs);
instead, it seems to work fine.
Thank you.
-
Here is one solution.
Code:
typedef vector<zone *> vecZonePtr;
vecZonePtr zone_route;
vecZonePtr::iterator iZone = find(zone_route.begin(), zone_route.end(), st);
if (iZone != zone_route.end())
{
// Something was found.
// Process data.
}
else
// Nothing was found.
Kuphryn