list container type

This is a discussion on list container type within the C++ Programming forums, part of the General Programming Boards category; 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); ...

  1. #1
    Funniest man in this seat minesweeper's Avatar
    Join Date
    Mar 2002
    Posts
    801

    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.

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Check up_flag.

    Kuphryn

  3. #3
    Funniest man in this seat minesweeper's Avatar
    Join Date
    Mar 2002
    Posts
    801
    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.

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help sorting a linked list. Beginner
    By scarlet00014 in forum C Programming
    Replies: 1
    Last Post: 09-27-2008, 06:16 PM
  2. deleting a node in linked list
    By BoneXXX in forum C Programming
    Replies: 18
    Last Post: 12-17-2007, 11:30 AM
  3. Pleas take a look & give a critique
    By sh3rpa in forum C++ Programming
    Replies: 14
    Last Post: 10-19-2007, 10:01 PM
  4. instantiated from here: errors...
    By advocation in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2005, 08:01 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21