beginner working with STL list

I am trying to write a program to perform the A star search algorithm.
I have the map and A Star Node defined as below, and I have declared some variables to work with.

class MapCell {
public:
short x_co;
short y_co;
bool obstacle;
string description;
};

class AStarNode {
public:
AStarNode *Parent;
short x_co;
short y_co;
short f;
short g;
short h;
};

#define MAXWIDTH 4
#define MAXHEIGHT 4

typedef AStarNode * AStarNodePtr;

list<AStarNodePtr> OpenList;
list<AStarNodePtr> CloseList;
list<MapCell> Path;
list<AStarNodePtr>::iterator iter;

MapCell grid[MAXWIDTH][MAXHEIGHT];


Now I want to write a function with return an AStarNode in a List which is the smallest f, either from OpenList or CloseList, so the function will take the arguments of the List and the Node.

However I am just a newbie and I have no idea how to access the data f and compare it. Could anyone give me some help please?

You could use the min element algorithm or keep the list sorted.
http://cplusplus.com/reference/algorithm/min_element/

Either way you should write a predicate to use with either the algorithm or the sort member function of list. By the way what is the *Parent attribute for? It'd be nice to see a bit more of what you are trying to do with the program. I'm glad to see that you are trying to use the STL. Let us know if you can't figure out what a predicate is. Look at the link and there is one overloaded version of the algorithm that requires a function pointer or functor which will compare two values and you'll use that program to access the f attribute for each. I wanted to say that your list could just be of objects but since you have that pointer attribute the copy construction and assignment would be an issue.
Thank you very much for your reply firstly.

Since I am writing the A Star Path Search algorithm, the *parent gives me the information of how the path is constructed by the Node I found, so at the end I will be at the Goal, and by looking back the parent, I can see the Path from Goal to Start.

To be honest, I have no idea what predicate is. Also, I still don't know how to access my data in AStarNode, as I was trying to do the follow, and have no idea how to write. In this code, I am comparing x_co and y_co to search a Node in a List.

bool SearchInList(list<AStarNodePtr> List, AStarNodePtr Node)
{
bool found = false;
for (iter=List.begin(); iter!=List.end(); ++iter)
{
*iter->x_co (Error)
*iter.x_co (Error)
}
return found;
}
Please use code-tags.
http://www.cplusplus.com/articles/firedraco1/

1
2
3
4
5
6
7
8
9
10
11
12
bool SearchInList(list<AStarNodePtr> List, AStarNodePtr Node)
{
  bool found = false;
  for (iter=List.begin(); iter!=List.end(); ++iter)
  {
    //you are missing the comparison here
    if((*iter).x_co == Node->x_co && (*iter).y_co == Node->y_co){
      found = true;
    }
  }
  return found;
}

}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
bool SearchInList(const list<AStarNodePtr>& List, AStarNodePtr Node) // don't copy - pass by reference
{
  bool found = false;

  // where did iter come from?  You have to actually give it a type and form a complete 
  // declaration.  Also don't keep calling end() within the loop.  It is pointless.  This is why
  // you need to learn to use functors and algorithms so that you don't retype the same
  // errors when writing your own loops.  Google the terms and start reading some articles.
  list<AStarNodePtr>::iterator iter(List.begin()), end(List.end());
  for(; iter !=end; ++iter)
  {
    // you are missing the comparison here
    if((*iter).x_co == Node->x_co && (*iter).y_co == Node->y_co){
      found = true;
      break; // don't keep looping if you found it already
    }
  }
  return found;
}
I don't see what that search function has to do with the original question but anyway good luck.
Topic archived. No new replies allowed.