Using find() for vectors

This is a discussion on Using find() for vectors within the C++ Programming forums, part of the General Programming Boards category; hi ppl... i have a vector of nodes called treenodes...ive just stored a node with the name 'pat' in it...everything ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    124

    Using find() for vectors

    hi ppl...

    i have a vector of nodes called treenodes...ive just stored a node with the name 'pat' in it...everything else is as defined below:
    Code:
    char word[256];
    ifstream in(filename);
    in >> word;                      //this has pat in it...
    
    //i make a new node with the name pat and push it onto the treenodes vector
    
    vector< TreeNode * >::iterator j = find(treenodes.begin(),treenodes.end(),word);
            cout << (*j)->name;
    the vector iterator line causes a runtime error...and Borland points me to string.h saying something about size of string...

    when i try to do the above by using a for loop it works...
    Code:
    for(vector< TreeNode * >::iterator j  = treenodes.begin(); j != treenodes.end(); j++)
            {
                    cout << (*j)->name;
            }
    what's wrong with the first one?

    Regards,

    Farooq

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    124
    ok i get it that the last argument to find is wrong...how can i get the iterator pointing to the node with the name as in word? i.e. i want the node with the same name as in word...if i find it i do something and if i don't find it i do something else...

    i want to put a condition like this inplace of word:
    Code:
    ((*j)->name == word)
    how to do this?

    Regards,

    Farooq
    Last edited by alvifarooq; 11-06-2004 at 11:44 AM.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    124
    nevermind...ill just use a map...pair will be node name and node itself...

    thanks anyway

    Farooq

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. could not find -lwsock32.lib
    By thomas_joyee in forum C++ Programming
    Replies: 8
    Last Post: 07-14-2008, 12:28 PM
  3. How to find O of threads ?
    By jabka in forum C Programming
    Replies: 3
    Last Post: 03-11-2008, 12:25 PM
  4. how do u find 2nd largest number??
    By juancardenas in forum C Programming
    Replies: 8
    Last Post: 02-14-2003, 07:28 AM
  5. Q: Recursion to find all paths of a maze
    By reti in forum C Programming
    Replies: 7
    Last Post: 11-26-2002, 08:28 AM

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