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. struct StudentNameIs {
  23. std::string key;
  24. StudentNameIs(const std::string& k) : key(k) {}
  25. bool operator()(const student& s) const { return key == s.name; }
  26. };
  27.  
  28. void display(list<student> &s)
  29. {
  30. list<student> :: iterator p;
  31. for(p=s.begin();p!=s.end();p++)
  32. cout<<p->name<<" "<<p->roll_number<<endl;
  33. }
  34. int main()
  35. {
  36. student s1("J",63),s2("P",29),s3("M",12),s4("S",71),s5("R",04);
  37. list<student> student_list;
  38.  
  39. student_list.push_back(s1);
  40. student_list.push_back(s2);
  41. student_list.push_back(s3);
  42. student_list.push_back(s4);
  43. student_list.push_back(s5);
  44.  
  45. display(student_list);
  46.  
  47. list<student> :: iterator q;
  48. string temp;
  49. list<student> :: iterator s,e;
  50. s=student_list.begin();
  51. e=student_list.end();
  52. //Accepting name of the student from the user to display roll number of it.
  53.  
  54. cout<<"Please Enter the name of student:"<<endl;
  55. cin>>temp;
  56.  
  57.  
  58. q=find_if(s, e, StudentNameIs(temp));
  59.  
  60. cout<<"The number is "<<q->roll_number<<endl;
  61. return 0;
  62. }
  63.  
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