fork download
  1. #include<iostream>
  2. #include<list>
  3. #include<algorithm>
  4. #include<vector>
  5.  
  6.  
  7. using namespace std;
  8.  
  9. class student
  10. {
  11. public:
  12. string name;
  13. int roll_number;
  14.  
  15. student(string n,int rn)
  16. {
  17. name=n;
  18. roll_number=rn;
  19. }
  20. };
  21.  
  22. void display(list<student> &s)
  23. {
  24. list<student> :: iterator p;
  25. for(p=s.begin();p!=s.end();p++)
  26. cout<<p->name<<" "<<p->roll_number<<endl;
  27. }
  28. int main()
  29. {
  30. student s1("J",63),s2("P",29),s3("M",12),s4("S",71),s5("R",04);
  31. list<student> student_list;
  32.  
  33. student_list.push_back(s1);
  34. student_list.push_back(s2);
  35. student_list.push_back(s3);
  36. student_list.push_back(s4);
  37. student_list.push_back(s5);
  38.  
  39. display(student_list);
  40.  
  41. list<student> :: iterator q;
  42. string temp;
  43. list<student> :: iterator s,e;
  44. s=student_list.begin();
  45. e=student_list.end();
  46. //Accepting name of the student from the user to display roll number of it.
  47.  
  48. cout<<"Please Enter the name of student:"<<endl;
  49. cin>>temp;
  50.  
  51.  
  52. q=find_if(s, e, [&](const student& s){return s.name == temp;});
  53.  
  54. cout<<"The number is "<<q->roll_number<<endl;
  55. return 0;
  56. }
  57.  
Success #stdin #stdout 0s 2992KB
stdin
S
stdout
J 63
P 29
M 12
S 71
R 4
Please Enter the name of student:
The number is 71