Thread: how to search a list

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    64

    how to search a list

    how do you perform a simple search on a list?

    is this the correct code?

    list<string> master;
    string target1;

    where = seek<string> (master.begin(), master.end(), target1);

    it gives me an error saying that i don't have WHERE defined. but i thought by assigning it, it took care of that.

  2. #2
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    you always need to define a variable before assigning it or otherwise using it
    hello, internet!

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    64
    what would i assign it as then? an iterator?

  4. #4
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    Code:
    list<string> lst;
    string str_to_find("AStringToFind");
    
    // Initialize the list with some items here
    
    
    // Find whatever you are looking for here
    list<string>::iterator iter = find( lst.begin(), lst.end(), str_to_find);
    
    // do whatever operation you wanted to do
    if ( iter == lst.end() ) {
        cout<<"Not found"<<endl;
    }
    else {
        // whatever you want to do: I remove the element
        lst.erase(iter);
    }

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    64
    the code makes sense to me and thank you for that.

    but it keeps saying that FIND is an undefined variable?

    i thought find was a reserved word?? can anyone help?

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    64
    i tried SEARCH as well...and same thing.

    is there something aside from the code that you gave me that i need. i have included list and cstdlib.

    ??

  7. #7
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    I´m not sure but try
    Code:
    #include <algorithm>

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recursion Revisited again, and again!
    By clegs in forum C++ Programming
    Replies: 93
    Last Post: 12-08-2007, 08:02 PM
  2. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  3. Big help in Astar search code...
    By alvifarooq in forum C++ Programming
    Replies: 6
    Last Post: 09-24-2004, 11:38 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM