fork download
  1. #include<iostream>
  2. #include<string>
  3. #include<unordered_map>
  4. using namespace std;
  5. class student{
  6. public:
  7. string firstname;
  8. string lastname;
  9. string rollno;
  10.  
  11. student(string f,string l, string n){
  12. firstname = f;
  13. lastname = l;
  14. rollno = n;
  15. }
  16. bool operator == (const student &s)const{
  17. return rollno == s.rollno;
  18. }
  19. };
  20.  
  21. class Hashfn{
  22. public:
  23. size_t operator()(const student &s)const{
  24. return s.firstname.length() + s.lastname.length();
  25. }
  26. };
  27. int main(){
  28.  
  29. unordered_map<student,int,Hashfn> student_map;
  30. student s1("pratek", "narang", "010");
  31. student s2("rahul", "kumar", "023");
  32. student s3("pratek", "gupta", "030");
  33. student s4("rahul", "kumar", "120");
  34.  
  35. //add student marks to hashmap
  36. student_map[s1] = 100;
  37. student_map[s2] = 80;
  38. student_map[s3] = 90;
  39. student_map[s3] = 70;
  40.  
  41. for(auto s:student_map){
  42. cout<<s.first.firstname<<" "<<s.first.rollno<<"marks"<<s.second<<endl;
  43. }
  44.  
  45. return 0;}
  46.  
Success #stdin #stdout 0s 4336KB
stdin
Standard input is empty
stdout
pratek 030marks70
pratek 010marks100
rahul 023marks80