Yes, it is possible, but doing so is fairly advanced and you still seem confused about using find_if().
I'll give you the advanced answer, but if this is homework be aware that your professor will probably fail you if you turn it in (because it is far advanced over what you know right now).
C++ is an "imperative" language. There is another paradigm called "functional". The STL provides some functional constructs. In the functional terminology, a "predicate" is a function that returns true or false based upon its arguments. There are unary predicates (takes one argument) and binary predicates (takes two arguments), etc.
The find_if() algorithm takes a unary predicate. The argument is of type "book" and the predicate returns true if the book's number field matches some specified number (hard-coded into the predicate function).
However, you want to use a binary predicate, where you specify both the book and the number to find. Fortunately, the STL provides a nifty little template function that takes a binary predicate and turns it into a unary predicate. (This is the functional part, because it is doing functional composition, called "currying".) The compositor function is called bind2nd() (that is, bind the second argument into the binary predicate to produce a unary predicate). It takes a little bit for this to sink-in.
So, the goal is to produce what the STL calls an "Adaptable Binary Predicate" (which is a fancy way of saying a binary template function that can be used to produce a unary function. It actually takes _three_ template arguments, because the return type must be specified also... but it still is just a binary predicate). Here's an "adaptable" binary predicate:
1 2 3 4 5 6 7
|
#include <functional>
struct BookNumber: public std::binary_function< Book, int, bool > {
bool operator () ( const Book &book, const int &number ) const {
return book.number == number;
}
};
|
Now that we have that out of the way, we can use it to actually find a specific book:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <list>
...
// The list of books
std::list<Book> books;
...
// Ask the user what book number to find
int number;
std::cout << "What number of book would you like to find? ";
std::cin >> number;
// Find and print every book with the specified number
int num_books_found = 0;
std::list<Book>::iterator result = books.begin();
while (true) {
result = std::find_if( result, books.end(), std::bind2nd( BookNumber(), number ) );
if (result == books.end()) break;
num_books_found++;
std::cout << setw( 3 ) << num_books_found << ": " << *result << std::endl;
result++;
}
|
I hope this hasn't been a waste of your time.