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.
#include<iostream>
#include<list>
#include<algorithm>
#include<vector>
usingnamespace 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 ?
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..
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.
. 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
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.