본문 바로가기
프로그래밍 기록/stl

count

by hominic 2023. 12. 9.
728x90
반응형

count

반응형
#include <iostream>
#include <algorithm>
using namespace std;

void main()
{
	const char *str="Oh baby baby,How was I supposed to know "
		"That something wasn't right here";
	size_t num;

	num=count(&str[0],&str[strlen(str)+1],'a');
	printf("이 문장에는 a가 %d개 있습니다.\n",num);
}

 

 

count_if

#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;

void main()
{
	const char *str="Oh baby baby,How was I supposed to know "
		"That something wasn't right here";
	size_t num;

	num=count_if(&str[0],&str[strlen(str)+1],bind2nd(greater<char>(),'t'));
	printf("이 문장에는 t보다 더 큰 문자가 %d개 있습니다.\n",num);
}

 

 

count2

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

const int NUM=2000;
const int RANGE=10;

void makerand(int &i)
{
	i=rand()%RANGE;
}

void main()
{
	vector<int> num(NUM);
	vector<int>::iterator it;
	int i;

	randomize();
	for_each(num.begin(),num.end(),makerand);
	for (i=0;i<RANGE;i++) {
		printf("%02d의 출현 회수 : %d\n",i,count(num.begin(),num.end(),i));
	}
}
728x90
반응형

'프로그래밍 기록 > stl' 카테고리의 다른 글

fill  (0) 2023.12.09
copy  (0) 2023.12.09
mstmatch  (0) 2023.12.09
equal  (0) 2023.12.09
for_each  (0) 2023.12.09

댓글