Skip to content

Latest commit

 

History

History
52 lines (42 loc) · 1.23 KB

count_if.md

File metadata and controls

52 lines (42 loc) · 1.23 KB

#count_if

namespace std {
  template <class InputIterator, class Predicate>
  typename iterator_traits<InputIterator>::difference_type
    count_if(InputIterator first, InputIterator last, Predicate pred);
}

###概要 条件を満たしている要素の数を数える。

###戻り値 [first,last) 内のイテレータ i について、pred(*i) != false であるイテレータの数を返す

###計算量 正確に last - first 回の述語の適用を行う

###実装例

template <class InputIterator, class Predicate>
typename iterator_traits<InputIterator>::difference_type
  count_if(InputIterator first, InputIterator last, Predicate pred) {
  typename iterator_traits<InputIterator>::difference_type ret = 0;
  for ( ; first != last; ++first)
    if (pred(*first)) ret++;
  return ret;
}

###使用例

#include <algorithm>
#include <iostream>
#include <vector>
 
int main() {
  std::vector<int> v = { 1,4,3,3,1,2,2,1 };
 
  // 値が 1 または 3 の要素がいくつあるかを数える
  auto count = std::count_if(v.begin(), v.end(), [](int x) { return x == 1 || x == 3; });
  std::cout << "count of 1 or 3: " << count << std::endl;
}
  • count_if[color ff0000]

###出力

count of 1 or 3: 5