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