count
Count appearances of value in range
Key Facts
Gyroscopic Couple: The rate of change of angular momentum () = (In the limit).- = Moment of Inertia.
- = Angular velocity
- = Angular velocity of precession.
Blaise Pascal (1623-1662) was a French mathematician, physicist, inventor, writer and Catholic philosopher.
Leonhard Euler (1707-1783) was a pioneering Swiss mathematician and physicist.
Definition
The count() algorithm is defined in the standard header <algorithm> and in the nonstandard backward-compatibility header <algo.h>.Interface
#include <algorithm> template < class InputIterator, class Type> typename iterator_traits<InputIterator>::difference_type count( InputIterator first, InputIterator last, const Type& val );Parameters:
Parameter | Description |
---|---|
first | An input iterator addressing the position of the first element in the range to be traversed |
last | An input iterator addressing the position one past the final element in the range to be traversed |
val | The value of the elements to be counted |
Return Value
Count algorithm returns the number of iteratorsi
in [first, last)
such that *i == value
.Complexity
The complexity is linear. Exactlylast - first
comparisons.
Example:
Example - count algorithm
Problem
This short program illustrates the functionality of count() algorithm.
Workings
#include <vector> #include <algorithm> #include <iostream> using namespace std; int main() { vector<int> v1; vector<int>::iterator Iter; v1.push_back(10); v1.push_back(20); v1.push_back(10); v1.push_back(40); v1.push_back(10); cout <<"v1 = ( " ; for (Iter = v1.begin(); Iter != v1.end(); Iter++) cout <<*Iter<<" "; cout <<")"<<endl; vector<int>::iterator::difference_type result; result = count(v1.begin(), v1.end(), 10); cout <<"The number of 10s in v2 is: "<<result<<"."<<endl; return 0; }
Solution
Output:
v1 = ( 10 20 10 40 10 )
The number of 10s in v2 is: 3.
The number of 10s in v2 is: 3.