count_if() Function in C++

This C++ program demonstrates the count_if algorithm. The count_if takes three arguments – of which the first two are iterators to the beginning and end of the container and the third argument is predicate. The container used here is map which stores name of the companies and their revenue. The predicate checks for companies with annual revenue greater than $125 billion.

Here is the source code of the C++ program demonstrates the count_if algorithm. 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 count_if() algorithm
  3.  */
  4. #include <iostream>
  5. #include <algorithm>
  6. #include <string>
  7. #include <map>
  8. #include <iomanip>
  9.  
  10. // Returns true for companies with revenue greater than $125 billion
  11. bool fun(std::pair <std::string, int> p)
  12. {
  13.     std::cout << std::setw(15) << std::left
  14.               << p.first << std::setw(10)
  15.               << p.second <<  std::endl;
  16.     if(p.second > 125)
  17.         return true;
  18.     else
  19.         return false;
  20. }
  21.  
  22. int main()  
  23. {
  24.     std::map <std::string, int> m;
  25.     int c;
  26.     m.insert(std::pair <std::string, int>("IBM", 104));
  27.     m.insert(std::pair <std::string, int>("HP", 120));
  28.     m.insert(std::pair <std::string, int>("Verizon", 115));
  29.     m.insert(std::pair <std::string, int>("WalMart", 469));
  30.     m.insert(std::pair <std::string, int>("AT&T", 127));
  31.  
  32.     std::cout << "Company\tRevene($ billion)" << std::endl;
  33.     std::cout << "-------\t-----------------" << std::endl;
  34.     c = count_if(m.begin(), m.end(), fun);
  35.     std::cout << "Number of companies = "
  36.               << c << std::endl;
  37.     return 0;
  38. }

$ a.out
Company	Revene($ billion)
-------	-----------------
AT&T           127       
HP             120       
IBM            104       
Verizon        115       
WalMart        469       
Number of companies = 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.