📄 cou_2233.htm
字号:
<HTML><TITLE>count, count_if</TITLE><BODY>
<A HREF="ref.htm"><IMG SRC="images/banner.gif"></A>
<P><STRONG>Click on the banner to return to the Class Reference home page.</STRONG></P>
<P>©Copyright 1996 Rogue Wave Software</P>
<H2>count, count_if</H2>
<HR><PRE> Algorithm</PRE><HR>
<A NAME="Summary"><H3>Summary</H3></A>
<P>Count the number of elements in a container that satisfy a given condition.</P>
<H3>Contents</H3>
<UL>
<A HREF="#Synopsis"><LI>Synopsis</LI></A>
<A HREF="#Description"><LI>Description</LI></A>
<A HREF="#Complexity"><LI>Complexity</LI></A>
<A HREF="#Example"><LI>Example</LI></A>
<A HREF="#Warnings"><LI>Warnings</LI></A>
</UL>
<A NAME="Synopsis"><H3>Synopsis</H3></A>
<PRE>#include <algorithm></PRE>
<PRE>
template <class InputIterator, class T, class Size>
void count(InputIterator first, InputIterator last,
const T& value, Size& n);
template <class InputIterator, class Predicate, class Size>
void count_if(InputIterator first, InputIterator last,
Predicate pred, Size& n);</PRE>
<A NAME="Description"><H3>Description</H3></A>
<P>The <B><I>count</B></I> algorithm compares <SAMP>value</SAMP> to elements in the sequence defined by iterators <SAMP>first</SAMP> and <SAMP>last</SAMP>, and increments a counting value <SAMP>n</SAMP> each time it finds a match. i.e., <B><I>count</B></I> adds to <SAMP>n</SAMP> the number of iterators <SAMP>i</SAMP> in the range <SAMP>[first, last)</SAMP> for which the following condition holds:</P>
<PRE>*i == value</PRE>
<P>The <B><I>count_if</B></I> algorithm lets you specify a predicate, and increments <SAMP>n</SAMP> each time an element in the sequence satisfies the predicate. That is, <B><I>count_if</B></I> adds to <SAMP>n</SAMP> the number of iterators <SAMP>i</SAMP> in the range <SAMP>[first, last)</SAMP> for which the following condition holds: </P>
<PRE>pred(*i) == true.</PRE>
<A NAME="Complexity"><H3>Complexity</H3></A>
<P>Both <B><I>count</B></I> and <B><I>count_if</B></I> perform exactly <SAMP>last-first</SAMP> applications of the corresponding predicate.</P>
<A NAME="Example"><H3>Example</H3></A>
<PRE>//
// count.cpp
//
#include <vector>
#include <algorithm>
#include <iostream.h>
int main()
{
int sequence[10] = {1,2,3,4,5,5,7,8,9,10};
int i=0,j=0,k=0;
//
// Set up a vector
//
vector<int> v(sequence,sequence + 10);
count(v.begin(),v.end(),5,i); // Count fives
count(v.begin(),v.end(),6,j); // Count sixes
//
// Count all less than 8
// I=2, j=0
//
count_if(v.begin(),v.end(),bind2nd(less<int>(),8),k);
// k = 7
cout << i << " " << j << " " << k << endl;
return 0;
}
Output : 2 0 7
</PRE>
<A NAME="Warnings"><H3>Warnings</H3></A>
<P>If your compiler does not support default template parameters then you need to always supply the <SAMP>Allocator</SAMP> template argument. For instance, you'll have to write:</P>
<PRE>vector <int, allocator></PRE>
<PRE></PRE><P>instead of:</P>
<PRE>vector <int></PRE>
<HR>
<A HREF="cop_4514.htm"><IMG SRC="images/prev.gif"></A> <A HREF="ref.htm#contents"><IMG SRC="images/toc.gif"></A> <A HREF="deq_4164.htm"><IMG SRC="images/next.gif"></A></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -