United States    
COMPAQ STORE | PRODUCTS | SERVICES | SUPPORT
| CONTACT US | SEARCH
C++
count (3C++std) - Tru64 UNIX
Standard C++ Library
Copyright 1996, Rogue Wave Software, Inc.

NAME

  count, count_if  - Count the number of elements in a container that satisfy
  a given condition.

SYNOPSIS

  #include <algorithm>
  template<class InputIterator, class T>
  iterator_trait<InputIterator>::distance_type
  count(InputIterator first, InputIterator last,
        const T& value);

  template <class InputIterator, class T, class Size>
  void count(InputIterator first, InputIterator last,
             const T& value, Size& n);

  template<class InputIterator, class Predicate>
  iterator_trait<InputIterator>::distance_type
  count_if(InputIterator first, InputIterator last,
           Predicate pred);

  template <class InputIterator, class Predicate, class Size>
  void count_if(InputIterator first, InputIterator last,
                Predicate pred, Size& n);

DESCRIPTION

  The count algorithm compares value to elements in the sequence defined by
  iterators first and last.  The first version of count  return the number of
  matches.  The second version increments a counting value n each time it
  finds a match.  i.e., count  returns (or adds to n) the number of iterators
  i in the range [first, last) for which the following condition holds:

  *i == value

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
  n that number of times).  That is, count_if returns (or adds to n) the
  number of iterators i in the range [first, last) for which the following
  condition holds:

  pred(*i) == true.  Both count and count_if perform exactly last-first
  applications of the corresponding predicate.

EXAMPLE

  //
  // count.cpp
  //
  // Does not demonstrate the partial specialization versions
  // of count and count_if
  //
   #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

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) will
  not be available.

  If your compiler does not support default template parameters then you need
  to always supply the Allocator template argument. For instance, you'll have
  to write:

  vector <int, allocator<int> >

  instead of:

  vector <int>

STANDARDS CONFORMANCE

  ANSI X3J16/ISO WG21 Joint C++ Committee
  

1.800.AT.COMPAQ

privacy and legal statement