Thread: some kind of problem with .find

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    26

    some kind of problem with .find

    alright so i'm making this program for an exercise in Beginning C++ Game Programming and i run it because i think it's all fine and it turns out it's not it pops up an error that says this "45 C:\Documents and Settings\Owner\Desktop\w00t c++ projects\Game list.cpp 'class std::vector<std::string, std::allocator<std::string> >' has no member named 'find' " and highlights this line
    Code:
    if (list.find(game) != string::npos)
    i think it might be that vectors can not use the find function because that is what it seems like...not sure though...here is my full code (it might be a tid bit messy)
    Code:
    // Game List
    
    #include <iostream>
    #include <string>
    #include <vector>
    
    using namespace std;
    
    int main()
    {
        string choice;
        string game;
        char quit = 'n';
        
        vector<string> list;
        
        vector<string>::iterator listIter;
        
        cout << "Welcome to the game list program. This program is made so you can";
        cout << "\nsort your games. type 'add' to add a game or type 'delete to";
        cout << "\ndelete a game or type 'quit' to quit the program.";
        
        while (quit != 'y')
        {
              cout << "Current list:";
              for (listIter = list.begin(); listIter != list.end(); ++listIter)
              {
                  cout << *listIter << endl;
              }
              
              cout << "What do you want to do? add, delete, quit: ";
              cin >> choice;
              
              if (choice == "add")
              {
                         cout << "Type in the name of the game you want to add ";
                         cin >> game;
                         list.push_back(game);
              }
              else if (choice == "delete")
              {
                         cout << "Type in the name of the game you want to delete ";
                         cin >> game;
                         
                         if (list.find(game) != string::npos)
                         {
                                             list.erase(game);
                         }
                         else
                         {
                                             cout << "I'm sorry, that game is not in the list";
                         }
              }
              else if (choice == "quit")
              {
                         cout << "Are you sure? <y/n>: ";
                         cin >> quit;
              }
        }
    }
    thanks, Rubiks14

  2. #2
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by Rubiks14
    it pops up an error that says this "45 C:\Documents and Settings\Owner\Desktop\w00t c++ projects\Game list.cpp 'class std::vector<std::string, std::allocator<std::string> >' has no member named 'find' " and highlights this line
    Code:
    if (list.find(game) != string::npos)
    your compiler is telling you the truth! vector has no find member. it's a seperate function

    use
    Code:
    // typed from memory, so may have syntax errors
    if (std::find(list.begin(), list.end(), game) !=  list.end())
    if this seems kinda weird to you initally (it did to me), bear in mind that having find as an external function allows you to use find on lists, vectors, queues, deques, normal pointer arrays (in fact, any class with some sort of iterator-style object that supports operator++)

    which is pretty damn cool.
    Last edited by ChaosEngine; 12-05-2005 at 05:29 PM. Reason: oops should be find() != end(), not string::npos
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    26
    thanks ChaosEngine that is what i was thinking after reading what it said...also thanks for the bit of code to fix it.

    now there is a problem with my erase member function which i think it the same thing (vectors can't use them) just dunno how to fix it...i'm guessing the command is similar to the one Chaos gave me
    Last edited by Rubiks14; 12-05-2005 at 05:33 PM.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    BTW, if you used a set instead of a vector the find method would be more efficient (O(log n) vs O(n)). If you used a unordered_set or hash_set instead, it could have even greater efficiency (O(1)), although with the regular set the list is automatically sorted. In the future, you may want to map the name of the game to some other data, in which case a map or unordered_map would be useful.

    >> there is a problem with my erase member function which i think it the same thing
    There is an erase function for vectors, but it takes an iterator that points to the position in the vector of the thing to erase. Save the result of the call to find in a vector<string>::iterator variable. Check to see if it is not equal to the end() iterator, and if it is not, then pass it to the erase function and you will successfully erase the value.
    Last edited by Daved; 12-05-2005 at 05:38 PM.

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    26
    will consider that but in the mean time i'm gonna try to stick with the book as much as i can so i don't get lost

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> will consider that but in the mean time i'm gonna try to stick with the book as much as i can so i don't get lost

    Good plan, just don't forget to look at those later on if they aren't mentioned in the book. (Note that I edited my previous post in regards to your erase problem).

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    alright so i'm making this program for an exercise in Beginning C++ Game Programming
    How do you like that book?

  8. #8
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    If you're in the market for a good STL reference book, I would definitely recommend "The C++ Standard Library: A Tutorial and Reference" by Nicolai M. Josuttis. So far, it's been a far better reference than anything I've found online.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with passing structs to a function.
    By darsh1120 in forum C Programming
    Replies: 7
    Last Post: 03-11-2008, 04:36 AM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  4. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 01:04 AM
  5. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM