.\" ident @(#)count.3 .\" Standard Template Library .\" $$RW_INSERT_HEADER "slyrs.man" .TH count 3C++ "02 Apr 1998" "Rogue Wave Software" "-" .ce2 Standard C++ Library Copyright 1998, Rogue Wave Software, Inc. .SH NAME \f2count\fP, \f2count_if\fP \ - Count the number of elements in a container that satisfy a given condition. .SH SYNOPSIS .br #include .br template .br typename iterator_traits::difference_type .br count(InputIterator first, InputIterator last, .RE .RS 6 const T& value); .RE .RS 0 template .br void count(InputIterator first, InputIterator last, .RE .RS 11 const T& value, Size& n); .RE .RS 0 template .br typename iterator_traits::difference_type .br count_if(InputIterator first, InputIterator last, .RE .RS 9 Predicate pred); .RE .RS 0 template .br void count_if(InputIterator first, InputIterator last, .RE .RS 14 Predicate pred, Size& n); .SH DESCRIPTION The count algorithm compares \f2value\fP to elements in the sequence defined by iterators \f2first\fP and \f2last\fP. The first version of count returns the number of matches. The second version increments a counting value \f2n\fP each time it finds a match. In other words, count returns (or adds to \f2n\fP) the number of iterators \f2i\fP in the range \f2[first, last)\fP for which the following condition holds: \f2*i == value\fP Type \f2T\fP must be EqualityComparable. .SH COMPLEXITY The count_if algorithm lets you specify a predicate, and returns the number of times an element in the sequence satisfies the predicate (or increments \f2n\fP that number of times). That is, count_if returns (or adds to \f2n\fP) the number of iterators \f2i\fP in the range \f2[first, last)\fP for which the following condition holds: \f2pred(*i) == true.\fP Both count and count_if perform exactly \f2last-first\fP applications of the corresponding predicate. .SH EXAMPLE .RE .RS 0 // .br // count.cpp .br // .br // Does not demonstrate the partial specialization versions .br // of count and count_if .br // .RE .RS 1 #include .br #include .br #include .RE .RS 0 using namespace std; .br .br int main() .RE .RS 1 { .RE .RS 2 int sequence[10] = {1,2,3,4,5,5,7,8,9,10}; .br int i=0,j=0,k=0; .RE .RS 3 // .br // Set up a vector .br // .RE .RS 2 vector v(sequence,sequence + 10); .RE .RS 0 .RE .RS 2 count(v.begin(),v.end(),5,i); // Count fives .br count(v.begin(),v.end(),6,j); // Count sixes .RE .RS 3 // .br // Count all less than 8 .br // I=2, j=0 .br // .RE .RS 2 count_if(v.begin(),v.end(),bind2nd(less(),8),k); .RE .RS 3 // k = 7 .RE .RS 0 .RE .RS 2 cout << i << " " << j << " " << k << endl; .br return 0; .RE .RS 1 } .br .RE .RS 0 Program Output .RE .RS 0 .br 2 0 7 .SH WARNINGS If your compiler does not support partial specialization, then the first version of both count and count_if (the one that returns the count) is not available. If your compiler does not support default template parameters, then you always need to supply the \f2Allocator\fP template argument. For instance, you have to write: \f2vector >\fP instead of: \f2vector \fP If your compiler does not support namespaces, then you do not need the using declaration for \f2std\fP.