count_if

범위에서 값을 지정 된 조건이 충족 요소 개수를 반환 합니다.

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

매개 변수

  • _First
    검색할 범위의 첫 번째 요소의 위치 주소 입력된 반복기입니다.

  • _Last
    검색할 위치 하나 과거 최종 요소 범위에 있는 주소 입력된 반복기입니다.

  • _Pred
    요소 계산 될 수 있으면 만족 하는 조건을 정의 하는 조건자 함수의 사용자 정의 개체입니다.단일 인수를 사용 하 고 반환 하는 조건자 true 또는 거짓.

반환 값

조건자에서 지정한 조건에 맞는 요소 수입니다.

설명

템플릿 함수 알고리즘의 일반화 된 , 조건자 교체 "같은 특정 값" 모든 조건자와.

예제

// 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 Samples)

표준 템플릿 라이브러리