thanks a lot for the advice...following is the code without the use of MAPS for now...i have a vector of strings to store the connections in the Node class and a vector of Nodes to store all the nodes...
you are right...there is a lot of unecessary looping involved through the vector of nodes and since the example i use right now is small it's ok...but if i were given a map with 500 cities...and i would have to do this for all of them, my program would have serious issues...
Code:
void readFile()
{
char check[20];
string Conn1;
string Conn2;
vector <Astar::Node *> CityNode;
ifstream fin ("inputfile.txt");
while (! fin.eof() )
{
fin >> check;
//IF THE FIRST WORD IN THE FILE IS A CITY
if (strcmp(check, "city") == 0)
{
Astar::Node *newNode = new Astar::Node;
fin >> newNode -> CityName;
fin >> newNode -> x_cord;
fin >> newNode -> y_cord;
CityNode.push_back(newNode);
}
//IF THE FIRST WORD IN THE FILE IS A CONN
if (strcmp(check, "conn") == 0)
{
fin >> Conn1;
fin >> Conn2;
for( vector< Astar::Node * >::iterator i = CityNode.begin(); i != CityNode.end(); i ++ )
{
if (strcmp(((*i)->CityName).c_str(), Conn1.c_str()) == 0)
{
((*i)->CityConnections).push_back(Conn2);
}
if (strcmp(((*i)->CityName).c_str(), Conn2.c_str()) == 0)
{
((*i)->CityConnections).push_back(Conn1);
}
}
}
//IF THE FIRST WORD IN THE FILE IS START
if (strcmp (check, "start") ==0)
{
fin >> Conn1;
for( vector< Astar::Node * >::iterator i = CityNode.begin(); i != CityNode.end(); i ++ )
{
if (strcmp(((*i)->CityName).c_str(), Conn1.c_str()) == 0)
{
(*i)->StartGoal = "start";
}
}
}
//IF THE FIRST WORD IN THE FILE IS GOAL
if (strcmp (check, "goal") ==0)
{
fin >> Conn1;
for( vector< Astar::Node * >::iterator i = CityNode.begin(); i != CityNode.end(); i ++ )
{
if (strcmp(((*i)->CityName).c_str(), Conn1.c_str()) == 0)
{
(*i)->StartGoal = "goal";
}
}
}
} //end of while
return;
}
later on in the Astar search this is what i have to do:
1) begin using the startnode (means i loop once to check which node in my vector has the start attribute)
2) do the search etc etc...
3) now look for the successors of this node (means i go through all the nodes again in the vector to check which nodes are connected to this node)
4) basically ill keep looping between step 2 and 3 until a solution is found...this means that my vector of nodes will be looped through once every time!!!
where does the MAP structure come in...maybe u'll have a better understanding now with the code already there...
thanks a lot again...
Regards,
Farooq