The use of Count and count_if in STL

Source: Internet
Author: User

I like crazy, and I found that if you really use STL functions like fish, you can basically save a lot of things...

When I went to class C ++ a few days ago, I thought this function was very interesting. Now I am using it very conveniently ,,,

Let's talk about the count_if function first. I think that all the comparable functions you can write can be found for you, my days, And, doesn't it save a lot of time?

# Include <stdio. h> # include <string. h> # include <stdlib. h> # include <math. h >#include <iostream >#include <algorithm> using namespace STD; int A [111]; bool CMP (int A) {if (a <0) return true; elsereturn false;} // you can set the CMP function on your own. This is too convenient. ...int main () {int N; while (scanf ("% d ", & N )! = EOF) {for (INT I = 0; I <n; I ++) scanf ("% d", & A [I]); int n = count_if (, A + N, CMP); printf ("% d \ n", n);} return 0 ;}



Count Function

#include <stdio.h>#include <string.h>#include <stdlib.h>#include <math.h>#include <iostream>#include <algorithm>using namespace std;int a[11];int main(){for(int i=0;i<10;i++)scanf("%d",&a[i]);int n=count(a,a+10,1);printf("%d\n",n);return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.