max_element() Function in C++

This C++ program demonstrates the max_element algorithm on a container. The max_element() function has two overloaded forms – one with a compare predicate as the third parameter and another without it. The program demonstrates both of the overloaded forms where one prints the element with maximum value in a vector and another using a compare function to find the pair element with maximum value in a map. The compare function compares the two pairs on the basis of their second arguments.

Here is the source code of the C++ program which demonstrates the max_element algorithm on a container. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C++ Program to demonstrate the max_element() algorithm on a container
  3.  */
  4. #include <iostream>
  5. #include <algorithm>
  6. #include <vector>
  7. #include <map>
  8. #include <string>
  9.  
  10. bool compare(std::pair <std::string, int> p, std::pair <std::string, int> q )
  11. {
  12.     return p.second < q.second;
  13. }
  14.  
  15. void print(const std::vector <int>& v)
  16. {
  17.     std::vector <int>::const_iterator i;
  18.     for(i = v.begin(); i != v.end(); i++)
  19.     {
  20.         std::cout << *i << " ";
  21.     }
  22.     std::cout << std::endl;
  23. }
  24.  
  25. void fun(std::pair <std::string, int> p)
  26. {
  27.     std::cout << p.first << "\t" << p.second << std::endl;
  28. }
  29.  
  30. int main()
  31. {
  32.     int arr[] = {10, 20, 30, 50, 40, 100, 70, 60, 80, 90};
  33.     std::vector <int> v(arr, arr + sizeof(arr)/sizeof(int));
  34.     std::map <std::string, int> marks;
  35.     std::pair <std::string, int> max;
  36.     marks["Alice"] = 60;
  37.     marks["Annie"] = 99;
  38.     marks["Harry"] = 90;
  39.     marks["Garry"] = 80;
  40.  
  41.     std::cout << "max_element() without predicate " << std::endl;
  42.     std::cout << "Vector : ";
  43.     print(v);
  44.     std::cout << "Maximum element = "
  45.               << *std::max_element(v.begin(), v.end())
  46.               << std::endl;
  47.     std::cout << "\nmax_element() with predicate" << std::endl;
  48.     std::cout << "Name\tMarks" << std::endl; 
  49.     for_each(marks.begin(), marks.end(), fun);
  50.     max = (*std::max_element(marks.begin(), marks.end(), compare));
  51.     std::cout << "Person with maximum marks = " << max.first
  52.               << ", Marks = " << max.second << std::endl;
  53. }

$ a.out
max_element() without predicate 
Vector : 10 20 30 50 40 100 70 60 80 90 
Maximum element = 100
 
max_element() with predicate
Name    Marks
Alice   60
Annie   99
Garry   80
Harry   90
Person with maximum marks = Annie, Marks = 99

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.