Searching a vector/struct for strings?

This is a discussion on Searching a vector/struct for strings? within the C++ Programming forums, part of the General Programming Boards category; How do you easily search a certain vector containing a struct, for a string that the user request using cin?...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    62

    Searching a vector/struct for strings?

    How do you easily search a certain vector containing a struct, for a string that the user request using cin?

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Create a function object that compares a string in a given struct with to a particular string, then use a std::for_each algorithm with the vector and that function object.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    62
    Quote Originally Posted by cpjust View Post
    Create a function object that compares a string in a given struct with to a particular string, then use a std::for_each algorithm with the vector and that function object.
    That was a little too complicated for an amateur level of c++, coould you show me what you mean in c++ language maybe things get more clear?

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    find_if is probably more useful for finding something than for_each.

    You have posted a lot of small questions asking to show you one thing or another.

    However, it is hard if we don't know what you know.

    Using the <algorithm> header requires understanding of iterators.
    Using more flexible algorithms (such as find_if) in addition requires understanding of predicates. To write your own predicates (function objects) you should be familiar with structs/classes, operator overloading etc.

    Therefore, there is no guarantee that any example demonstrating the usage of find_if (or other algorithms) wouldn't be way beyond your skill level.

    If all you know is how to loop over the vector and access each element in it, it might be easier for you to write the search loop manually:
    Code:
    for each item in vector:
         if item.somestring == string_to_find:
              you've found it!
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,455
    If you must find something that can be stored anywhere, a map is probably a better choice.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    May 2008
    Posts
    62
    Quote Originally Posted by Elysia View Post
    If you must find something that can be stored anywhere, a map is probably a better choice.
    Elysia, you have been following my threads, thank you. Now if you wanted to use a map to find something as in my prev. examples, how would you do?
    Code:
        struct data 
        {
               string name;
               string title;
               string author;
        };
    
    
        vector<data> books;

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,456
    Do you want to search for name, title, or author, or perhaps the user has the option of any one of them, or a combination of them?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    May 2008
    Posts
    62
    Quote Originally Posted by laserlight View Post
    the user has the option of any one of them, or a combination of them
    That's the case.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,456
    In that case a map is not appropriate (though you could use several maps, but let's examine that option later).

    Consider anon's simple loop idea if you find find_if and predicates a little too tough at the moment. Try writing a loop that loops through the vector and compares each element's name member with some string.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,455
    If you want to find any of those, then use a key of "std::string", since all of those are strings.
    Then just add the appropriate thing you want to find as the key and associate it with a value.
    A map is basically like a database where you say "Hey, find this title" and it finds the value associated with it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,456
    If you want to find any of those, then use a key of "std::string", since all of those are strings.
    Then just add the appropriate thing you want to find as the key and associate it with a value.
    A map is basically like a database where you say "Hey, find this title" and it finds the value associated with it.
    The thing is, a map serves as an index. With multiple possible combinations of members to search, one needs to build an index for each possible combination (or at least the most likely combinations). As such, there must be many maps. However, we only want one list of objects, so the maps must map strings to pointers (or iterators or indices).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,455
    If you have the memory, I would think it would be faster to index several maps than to search through a vector.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,456
    If you have the memory, I would think it would be faster to index several maps than to search through a vector.
    Yes, and it would be faster and more flexible to use SQLite instead of doing it yourself. Columns and combinations of columns could then be indexed or have their indices removed as tests show what optimisations are needed.

    My point here is that Glauber seems interested in learning rather than actual application, so we should walk him/her through the basics first.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,455
    Right, I suppose.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    May 2008
    Posts
    62
    Quote Originally Posted by anon View Post
    find_if is probably more useful for finding something than for_each.

    You have posted a lot of small questions asking to show you one thing or another.

    However, it is hard if we don't know what you know.

    Using the <algorithm> header requires understanding of iterators.
    Using more flexible algorithms (such as find_if) in addition requires understanding of predicates. To write your own predicates (function objects) you should be familiar with structs/classes, operator overloading etc.

    Therefore, there is no guarantee that any example demonstrating the usage of find_if (or other algorithms) wouldn't be way beyond your skill level.

    If all you know is how to loop over the vector and access each element in it, it might be easier for you to write the search loop manually:
    Code:
    for each item in vector:
         if item.somestring == string_to_find:
              you've found it!
    How would that be in the following example?:

    Code:
        struct data 
        {
               string name;
               string title;
               string author;
        };
    
    
        vector<data> books;
    
        cin >> search_input;
    
                for (int i = 0; i < books.name.size(); i++)                    //Say that you want to search the name member of the struct "data", by comparing the user input to it.
                    if (books.name == search_input)
                             cout << "It's there."

Page 1 of 2 12 LastLast
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, 09: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

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