How do you easily search a certain vector containing a struct, for a string that the user request using cin?
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?...
How do you easily search a certain vector containing a struct, for a string that the user request using cin?
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.
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.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
If you must find something that can be stored anywhere, a map is probably a better choice.
For information on how to enable C++11 on your compiler, look here.
よく聞くがいい!私は天才だからね! ^_^
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
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
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.
For information on how to enable C++11 on your compiler, look here.
よく聞くがいい!私は天才だからね! ^_^
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).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.
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
If you have the memory, I would think it would be faster to index several maps than to search through a vector.
For information on how to enable C++11 on your compiler, look here.
よく聞くがいい!私は天才だからね! ^_^
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.If you have the memory, I would think it would be faster to index several maps than to search through a vector.
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
Right, I suppose.
For information on how to enable C++11 on your compiler, look here.
よく聞くがいい!私は天才だからね! ^_^
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."