Confusion in find function of algorithm

Hi,
Here is my program definition:
Create a student class that include a student's first name and his roll_number.Create five objects of this class and store them in a list.Write a program using this list to display the student name if the roll_number is going and voice-versa.

I have written following code.
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include<iostream>
#include<list>
#include<algorithm>
#include<vector>


using namespace std;

class student
{
    public:
    string name;
    int roll_number;

    student(string n,int rn)
    {
        name=n;
        roll_number=rn;
    }
};

void display(list<student> &s)
{
    list<student> :: iterator p;
    for(p=s.begin();p!=s.end();p++)
    cout<<p->name<<" "<<p->roll_number<<endl;
}
int main()
{
    student s1("J",63),s2("P",29),s3("M",12),s4("S",71),s5("R",04);
    list<student> student_list;

    student_list.push_back(s1);
    student_list.push_back(s2);
    student_list.push_back(s3);
    student_list.push_back(s4);
    student_list.push_back(s5);

   display(student_list);

   list<student> :: iterator q;
   string temp;
    list<student> :: iterator s,e;
    s=student_list.begin();
    e=student_list.end();
//Accepting name of the student from the user to display roll number of it.

   cout<<"Please Enter the name of student:"<<endl;
   cin>>temp;


   q=find(s->name,e->name,temp);

   cout<<"The number is "<<q->roll_number<<endl;
    return 0;
}


In find function i want to apply it to "name" member of class "student". How it is possible ?
This is really a job for std::map. If you must search a list, the way to do that is to provide find_if() with a functor that compares student names:

q=find_if(s, e, [&](const student& s){return s.name == temp;});

online demo: http://ideone.com/ojg0cT

or (if you only have an old compiler), you could write that out by hand:

1
2
3
4
5
6
7
8
9
struct StudentNameIs {
   std::string key;
   StudentNameIs(const std::string& k) : key(k) {}
   bool operator()(const student& s) const { return key == s.name; }
};

...

   q=find_if(s, e, StudentNameIs(temp));


demo: http://ideone.com/qnNyQC

std::find() would only work for you if you provide an operator== that compares students and returns true if their names match, which may or may not be a good thing..

1
2
3
4
5
6
7
8
bool operator==(const student& lhs, const student& rhs)
{
    return lhs.name == rhs.name;
}

...

   q=find(s, e, student(temp, 0));

demo: http://ideone.com/DLLNkV
Last edited on
Okay. I got some of your point.But let me be frank i didn't understand what you have done in this statement [code]q=find_if(s, e, [&](const student& s){return s.name == temp;});[\code]
what is meaning of
[&]
this. and
return s.name == temp;
really i didn't got your point.I'm beginner in c++. So will you provide some detailed information.It will be good enough for me to develop my self.And kindly suggest some reference material if it is available of it.
The first example uses modern (2011) language features. If you don't have them in your books, fall back to the second and third examples.
Thank you
cubbi
. I understood third. It works for me.But i am interested for the first one also. Because my book is little bit old for use the first method is not covered in my book.I want to learn it. Call you give some reference material on that topic. please?
For second what will the
predicate
function do in the find_if function.
Last edited on
The predicate is a function object that you, the programmer, are supplying. It is supposed to return true when invoked with the record you're looking for and false when invoked with any other record.
find_if() will keep call your predicate on every element of the list until it returns true.

The second example shows how such predicates were written in 1998: as a class with an overloaded function-call operator. It returns true when called with a record whose .name equals the value passed in the constructor.

The first example shows the new shorthand syntax which does the same thing (it creates a class with an overloaded function-call operator). Google "C++ lambda expression" for details.

There are a few more ways to write predicates, since this is such a common task. The boost library has some convenient ones.
Topic archived. No new replies allowed.