다음을 통해 공유


count_if

Returns the number of elements in a range whose values satisfy a specified condition.

template<class InputIterator, class Predicate> 
   typename iterator_traits<InputIterator>::difference_type count_if( 
      InputIterator _First,  
      InputIterator _Last, 
      Predicate _Pred 
   );

매개 변수

  • _First
    An input iterator addressing the position of the first element in the range to be searched.

  • _Last
    An input iterator addressing the position one past the final element in the range to be searched.

  • _Pred
    User-defined predicate function object that defines the condition to be satisfied if an element is to be counted. A predicate takes single argument and returns true or false.

반환 값

The number of elements that satisfy the condition specified by the predicate.

설명

This template function is a generalization of the algorithm count, replacing the predicate "equals a specific value" with any predicate.

예제

// alg_count_if.cpp
// compile with: /EHsc
#include <vector>
#include <algorithm>
#include <iostream>

bool greater10(int value)
{
    return value >10;
}

int main()
{
    using namespace std;
    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 result1;
    result1 = count_if(v1.begin(), v1.end(), greater10);
    cout << "The number of elements in v1 greater than 10 is: "
         << result1 << "." << endl;
}
  

요구 사항

헤더: <algorithm>

네임스페이스: std

참고 항목

참조

count_if(STL 샘플)

표준 템플릿 라이브러리