C ++ STL's count function

count: count the number of times a value occurs in a sequence 

count_if: count the number of times a predicate match in the sequence

count and count count_if function is a function of, first look at the count function:

count function is a function of: a container statistical value equal to the number of elements.

Look at the function parameters:
COUNT (First, last, value); First is the first iterator containers, last iterator is at the end of the container, value is to ask elements.

I might say the less detailed, look at an example:
you n numbers (n <= 1000), to give you a number m, you ask: the number of digital m occurs in the n numbers.

See this question, we will think of using the sort function with equal_range + (n ranges of around 10,000 --- 100,000), but n <= 1000 small amount of data, so we can directly use the count function, here we note that: the complexity of the count function is linear, the worst case is O (n). This question is very simple, so we will soon be able to write the code:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <vector>
 5 using namespace std;
 6 int main()
 7 {
 8     int n;
 9     vector <int> V;
10     cin>>n;
11     for(int i=0;i<n;i++)
12     {
13         int temp;
14         cin>>temp;
15         V.push_back(temp);
16     }
17     int ask;
18     while(cin>>ask)
19     {
20         int num=count(V.begin(),V.end(),ask);
21         cout<<num<<endl;
22     }
23     return 0;
24 }

However, if we want to use statistical functions STL odd number of 1-10, how to do it?

So, we need to use count_if function, which is a very useful function, we have to focus on it.

Look at the parameters count_if function:
count_if (First, last, value, cmp); First, led by the iterator, last for the end iterator, value for the element you want to query, cmp as the comparison function.

In fact, the comparison function cmp is the core of the whole count_if function, comparison function cmp is written in the programming of the people, the return value is a Boolean, I believe after reading my example, you can understand the application of this function. Example: count the number of odd-numbered 1-10 (my code):

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <vector>
 5 using namespace std;
 6 bool comp(int num)
 7 {
 8     return num%2;
 9 }
10 int main()
11 {
12     vector <int> V;
13     for(int i=1;i<=10;i++)
14         V.push_back(i);
15     cout<<count_if(V.begin(),V.end(),comp)<<endl;
16     return 0;
17 }

Example:

 1 //统计成绩大于90
 2 
 3 #include <iostream>
 4 #include <cstdio>
 5 #include <cstring>
 6 #include <vector>
 7 #include <algorithm>
 8 using namespace std;
 9 struct student
10 {
11     string name;
12     int score;
13 };
14 bool compare(student a)
15 {
16     return 90<a.score;
17 }
18 int main()
19 {
20     int n;
21     cin>>n;
22     vector<student> V;
23     for(int i=0;i<n;i++)
24     {
25         student temp;
26         cin>>temp.name>>temp.score;
27         V.push_back(temp);
28     }
29     cout<<count_if(V.begin(),V.end(),compare)<<endl;
30     return 0;
31 }

 

Original link: https: //www.cnblogs.com/Roni-i/p/8675528.html

Guess you like

Origin www.cnblogs.com/jaszzz/p/12635244.html