기술 블로그

count(), count_if() 본문

C++ STL

count(), count_if()

parkit 2019. 5. 20. 13:27
728x90
반응형

http://www.cplusplus.com/reference/algorithm/count/




count() : 배열, 벡터 등 원하는 값(원소)의 개수를 찾아줌. string, char, int 모두 가능하다.


count_if() : 조건에 맞는 배열, 벡터 등 원하는 값(원소)의 개수를 찾아줌. string, char, int 모두 가능하다.




count(시작점, 끝점, 찾을 값);


count_if(시작점, 끝점, bool 함수);







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <bits/stdc++.h>
 
using namespace std;
 
bool isEven(int i)
{
    return (i % 2 == 0 );
}
 
int main(void)
{
    vector<int> v = { 1234523439123 };
 
    printf("%d\n", count(v.begin(), v.end(), 3));
 
    printf("%d\n", count_if(v.begin(), v.end(), isEven));
 
    vector<string> s = { "ABC""DEF""ABC""EFG" };
 
    printf("%d\n", count(s.begin(), s.end(), "ABC"));
 
    return 0;
}
cs


















728x90
반응형

'C++ STL' 카테고리의 다른 글

vector struct 구조체 정렬(3개 이상)  (0) 2019.06.29
search()  (0) 2019.05.20
equal()  (0) 2019.05.20
is_sorted()  (0) 2019.05.10
lower_bound(), upper_bound()  (0) 2019.05.05