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
반응형
댓글