Help with file reading...[Urgent]

This is a discussion on Help with file reading...[Urgent] within the C++ Programming forums, part of the General Programming Boards category; ok ill do that...can u think abou the earlier problem...it's before the 'main function' post... Thanks Farooq...

  1. #16
    Registered User
    Join Date
    Sep 2004
    Posts
    124
    ok ill do that...can u think abou the earlier problem...it's before the 'main function' post...

    Thanks

    Farooq

  2. #17
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Ok, one question I had that I don't remember the answer to is are all the connections and cities in the file randomly? The reason I ask is because it makes a big difference if you can read in a connection before the cities it refers to have been read in.

    Second, just a little note. It is easy to compare strings, you don't have to use strcmp. So you can do this:
    Code:
    if ((*i)->CityName == Conn1)
    instead of this
    Code:
    if (strcmp(((*i)->CityName).c_str(), Conn1.c_str()) == 0)
    That only works for the string class, not the character arrays like char check[20].



    Finally the basic idea of using the map is to replace your CityNode vector. So you can use this
    Code:
    map<string, Astar::Node *> CityNode;
    instead of this
    Code:
    vector <Astar::Node *> CityNode;
    When you insert, instead of using push_back, you do this:
    Code:
    CityNode.insert(make_pair(newNode -> CityName, newNode));
    Then, instead of looping through the vector when reading in a connection, you simply lookup the city in the map. You can read up on the member function find() and how to use it.. it is probably most appropriate for your case. Another potentially dangerous solution is to just use the subscript. However, if the string does not exist in your map already, THIS WILL CRASH YOUR PROGRAM!
    Code:
    CityNode[conn1]->Connections.push_back(conn2);
    CityNode[conn2]->Connections.push_back(conn1);
    I don't want to write all the code for you, so you can figure out how to use find instead. It is safer, the code above just gives you the idea.

  3. #18
    Registered User
    Join Date
    Sep 2004
    Posts
    124
    hmmm...i see what u mean...the city names and connections in the file are not random...once you import the file that is the question you have to solve until well you run the program again and import some other file...

    so you're saying i can simply use find() to basically automatically select use the cityname to find its corresponding node and then in that node i can check all my connections (which are already in the form of a vector of strings)...

    i have one question here:

    - wouldn't find() do the looping through the whole map as well...only that it's a member function so it's neater...i mean is there a performance benefit from the looping i have to do...

    subscript use is also a good idea...i can probably use this easily because i know that the cities in the connection will already be defined as nodes...


    best regards,

    Farooq

  4. #19
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Quote Originally Posted by alvifarooq
    wouldn't find() do the looping through the whole map as well...only that it's a member function so it's neater...i mean is there a performance benefit from the looping i have to do...
    The STL map gaurantees faster lookup time than the linear search you are using. Most implementations use a binary tree, so it isn't looping through one at a time, but traversing the tree. The map is O(log n) where as the unsorted vector is O(n).

    As I mentioned before, there are other options. A non-standard hash_map will have on average constant lookup time - even faster than the binary tree. If all the cities were read in first, you could use a vector and then sort it using a sort algorithm, followed by a binary_search to find the appropriate node. These are all better than a linear search like you were doing. I think the map solution is fine.

    And defensive programming is a good idea. I would recommend using the find() method instead of the subscript. It wouldn't be too hard to implement given the code you already have, and it is safer if you check it before you use it.

  5. #20
    Registered User
    Join Date
    Sep 2004
    Posts
    124
    here's the new code...u're right this is much better...ive implemented the same function as before this way...now i'll be using this in the Astar search...ill see if it works there better than vectors...

    Code:
    //Reads the contents of the input file
           void readFile()
            {
               string check;
               string Conn1;
               string Conn2;
    
               map <string, Astar::Node *> CityNode;
    
               ifstream fin ("inputfile.txt");
    
               while (! fin.eof() )
               {
                  fin >> check;
    
           if (check == "city")
           {
              Astar::Node *newNode = new Astar::Node;
              fin >> newNode -> CityName;
              fin >> newNode -> x_cord;
              fin >> newNode -> y_cord;
              CityNode.insert(make_pair(newNode -> CityName, newNode));
           }
    
           if (check == "conn")
           {
              fin >> Conn1;
              fin >> Conn2;
    
              map <string, Astar::Node *>::iterator i = CityNode.find(Conn1);
              ((i->second)->CityConnections).push_back(Conn2);
    
              i = CityNode.find(Conn2);
              ((i->second)->CityConnections).push_back(Conn1);
    
           }
    
           if (check == "start")
           {
              fin >> Conn1;
    
              map <string, Astar::Node *>::iterator i = CityNode.find(Conn1);
              (i->second)->StartGoal = "start";
    
           }
    
           if (check == "goal")
           {
              fin >> Conn1;
    
              map <string, Astar::Node *>::iterator i = CityNode.find(Conn1);
              (i->second)->StartGoal = "goal";  
           }       
    
        }//END OF WHILE
    
    
        for( map <string, Astar::Node *>::iterator i = CityNode.begin(); i != CityNode.end(); i ++ )
        {
           Form1->ListBox1->Items->Add( ((i->second)-> CityName).c_str() );
           Form1->ListBox1->Items->Add( ((i->second)-> CityConnections.size()) );
           Form1->ListBox1->Items->Add( (i->second)-> x_cord );
           Form1->ListBox1->Items->Add( (i->second)-> y_cord );
           Form1->ListBox1->Items->Add( ((i->second)-> StartGoal).c_str() );
           Form1->ListBox1->Items->Add( "----" );
        }
     
        return;
    
    }
    Thanks a lot again...really mean it!

    Regards,

    Farooq
    Last edited by alvifarooq; 09-18-2004 at 08:01 PM.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

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