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
If you wish to look at all C++ Programming examples, go to C++ Programs.

advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification 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
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 20s–40s and exploring new directions in your career, I also offer mentoring. Learn more here.