Thread: Searching a vector/struct for strings?

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,403
    Close, but not quite. The main problem is that the index i loops from 0 to one less the vector size. books.name does not exist, since vectors do not have a name member. A more minor problem is that the type of the index should be vector<data>::size_type, not int. Then, after you have found a matching element, you may wish to break out of the loop (or you may not, it depends). Consequently, a more likely solution would be:
    Code:
    vector<data> books;
    // ...
    cin >> search_input;
    
    for (vector<data>::size_type i = 0; i < books.size(); ++i)
    {
        if (books[i].name == search_input)
        {
            cout << "Found with title: " << books[i].title << endl;
            break;
        }
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,611
    Glauber, do you even have a compiler or anything like that?

    A vector is supposed to model an array, which is simply a sequential collection of items.

    So if you had various books and you'd want to find a particular one, you'd have to use a search algorithm. One of the posters showed you pseudo-code for the simplest algorithm, linear search. Just move through the collection one by one and compare the item to the search criterion. True, you might be comparing only part of the items to what you're looking for but you need to understand how to access info from the collection.

    But the idea in arrays is that each item in the collection has a index so you can look it up again right away.

    my_library[]:
    Snow Crash
    Neal Stephenson
    ----------------------
    Inca Gold
    Clive Cussler
    ----------------------
    The Hitchhiker's Guide to the Galaxy
    Douglas Adams
    ----------------------
    The Rainmaker
    John Grisham
    -----------------------

    The middle books, Inca Gold and Hitchhiker's Guide, are at indices 1 and 2 if we start from zero. So if you wanted to do something with a part of either of those books (or any book) you need to look it up. Then you can access the members of a book record, like the author name or title.

    Now, in linear searching, you might stop the search on the first match, or keep searching through the whole space and return a collection of the matches (this works well for more relaxed search criteria, such as "every Stephen King novel here").


    You really do need a book though. We are not a classroom!

  3. #18
    Registered User
    Join Date
    May 2008
    Posts
    62
    Quote Originally Posted by laserlight View Post
    Close, but not quite. The main problem is that the index i loops from 0 to one less the vector size. books.name does not exist, since vectors do not have a name member. A more minor problem is that the type of the index should be vector<data>::size_type, not int. Then, after you have found a matching element, you may wish to break out of the loop (or you may not, it depends). Consequently, a more likely solution would be:
    Code:
    vector<data> books;
    // ...
    cin >> search_input;
    
    for (vector<data>::size_type i = 0; i < books.size(); ++i)
    {
        if (books[i].name == search_input)
        {
            cout << "Found with title: " << books[i].title << endl;
            break;
        }
    }
    Thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. Visual C++ 2005 linking and file sizes
    By Rune Hunter in forum C++ Programming
    Replies: 2
    Last Post: 11-12-2005, 10:41 PM
  4. Searching and matching strings: segmentation fault
    By Smola in forum C Programming
    Replies: 18
    Last Post: 07-11-2005, 12:25 AM