DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK

# count(3C++)

count -- count the elements of an array that satisfy a given criterion

## Synopsis

```   #include <stddef.h>

template<class T>
ptrdiff_t count(
const T& val,
const T* b,
const T* e
);
template <class T>
ptrdiff_t count_r(
int (*rel)(const T*,const T*),
const T& val,
const T* b,
const T* e
);
template <class T>
ptrdiff_t count_p(
int (*pred)(const T*),
const T* b,
const T* e
);
```

## Assumptions

(1) For the plain version, T::operator== defines an equivalence relation on T.

(2) For the relational version, rel defines an equivalence relation on T.

## Description

These functions count elements that satisfy some criterion.

```   template <class T>
ptrdiff_t count(
const T& val,
const T* b,
const T* e
);
```

Counts elements equal to val, as determined by T::operator==.

```   template <class T>
ptrdiff_t count_r(
int (*rel)(const T*,const T*),
const T& val,
const T* b,
const T* e
);
```

Like count, but uses rel to test for equality. That is, if p is a pointer into the array, then *p is counted if rel(p,&val)==0.

```   template <class T>
ptrdiff_t count_p(
int (*pred)(const T*),
const T* b,
const T* e
);
```

Counts elements that satisfy the predicate. That is, if p is a pointer into the array, then *p is counted if pred(p) is true.

## Complexity

If N is the size of the array, then complexity is O(N). Exactly N tests of the relation are done.

## Notes

Because a Block (see Block(3C++)) can always be used wherever an array is called for, Array Algorithms can also be used with Blocks. In fact, these two components were actually designed to be used together.

## References

```   Array_alg(3C++)
Block(3C++)
```