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.
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?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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).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.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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."