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
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.