min_element() Function in C++

This C++ program demonstrates the min_element algorithm on a container. The min_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 minimum value in a vector and another using a compare function to find the pair element with minimum 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 min_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 min_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[] = {40, 80, 50, 30, 10, 70, 60, 100, 20, 90};
  33.     std::vector <int> v(arr, arr + sizeof(arr)/sizeof(int));
  34.     std::map <std::string, int> rank;
  35.     std::pair <std::string, int> min;
  36.     rank["John"] = 10;
  37.     rank["Chris"] = 5;
  38.     rank["Mark"] = 2;
  39.     rank["Henry"] = 7;
  40.  
  41.     std::cout << "min_element() without predicate " << std::endl;
  42.     std::cout << "Vector : ";
  43.     print(v);
  44.     std::cout << "Minimum element = "
  45.               << *std::min_element(v.begin(), v.end())
  46.               << std::endl;
  47.     std::cout << "\nmin_element() with predicate" << std::endl;
  48.     std::cout << "Name\tMarks" << std::endl; 
  49.     for_each(rank.begin(), rank.end(), fun);
  50.     min = (*std::min_element(rank.begin(), rank.end(), compare));
  51.     std::cout << "Person with best rank = " << min.first
  52.               << ", Rank = " << min.second << std::endl;
  53. }

$ a.out
min_element() without predicate 
Vector : 40 80 50 30 10 70 60 100 20 90 
Minimum element = 10
 
min_element() with predicate
Name    Marks
Chris   5
Henry   7
John    10
Mark    2
Person with best rank = Mark, Rank = 2

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

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

If you find any mistake above, kindly email to [email protected]

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.