¹Ýº¹ÀÚ ±¸°£¿¡¼ ÁöÁ¤ÇÑ °ª°ú ÀÏÄ¡ÇÏ´Â ¿ä¼ÒÀÇ °³¼ö¸¦ ¼¾´Ù. °ªÀ» ÃëÇÏ´Â ¹öÀü°ú Á¶°ÇÀÚ¸¦ ÃëÇÏ´Â ¹öÀüÀÌ °¢°¢ µû·Î Á¤ÀǵǾî ÀÖ´Ù.
size_t count(InIt first, InIt last, const T& val);
size_t count_if(InIt first, InIt last, UniPred F);
¸®ÅϰªÀº Á¶°ÇÀ» ¸¸Á·ÇÏ´Â ¿ä¼ÒÀÇ °³¼öÀ̸ç ÀÏÄ¡ÇÏ´Â ¿ä¼Ò°¡ ¾øÀ¸¸é 0ÀÌ ¸®ÅϵȴÙ. ´ÙÀ½ ¿¹Á¦´Â ³ë·¡ °¡»ç ¹®ÀÚ¿¿¡¼ a¹®ÀÚÀÇ ÃâÇö ȸ¼ö¸¦ ¼¾´Ù.
¿¹ Á¦ : 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´Â ¹Ýº¹ÀÚ ±¸°£À» Â÷·Ê´ë·Î ¼øÈ¸ÇÏ¸é¼ ¸Å ¿ä¼Ò°¡ val°ú °°ÀºÁö == ¿¬»êÀ¸·Î ºñ±³ÇÏ¿© ÀÏÄ¡ÇÏ´Â ¿ä¼Ò°¡ ¹ß°ßµÉ ¶§¸¶´Ù ȸ¼ö¸¦ 1 Áõ°¡½ÃŰ°í ¼øÈ¸¸¦ ¸¶Ä¥ ¶§ Á¶»çÇÑ È¸¼ö¸¦ ¸®ÅÏÇÑ´Ù. À§ ¹®ÀÚ¿¿¡´Â a°¡ ¸ðµÎ 5°³ ÀÖÀ¸¹Ç·Î countÀÇ ½ÇÇà °á°ú´Â 5°¡ µÉ °ÍÀÌ´Ù. count´Â == ¿¬»êÀ¸·Î ÀÏÄ¡ Á¶°ÇÀ» ÆÇ´ÜÇÏÁö¸¸ count_if´Â ´ÜÇ× Á¶°ÇÀÚ °´Ã¼°¡ ÀÏÄ¡ Á¶°ÇÀ» ÆÇ´ÜÇϹǷΠÁ» ´õ ´Ù¾çÇÑ Á¶°ÇÀ» Á¡°ËÇÒ ¼ö ÀÖ´Ù.
¿¹ Á¦ : 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);
}
ÀÌÇ× Á¶°ÇÀÚÀÎ greaterÀÇ µÎ ¹øÂ° Àμö¸¦ bind2nd ¾î´ðÅÍ·Î 't'·Î °íÁ¤ÇÏ¿© 't'º¸´Ù Å« ¹®ÀÚÀÇ °³¼ö¸¦ ¼¼¾î º¸¾Ò´Ù. °á°ú´Â 7ÀÌ´Ù. ´ÙÀ½ ¿¹Á¦´Â CÀÇ ³¼ö ¹ß»ý±â ¼º´ÉÀ» Å×½ºÆ®Çϴµ¥ 0~10±îÁöÀÇ ³¼ö¸¦ ¹«ÀÛÀ§·Î 2000°³ ¸¸µé¾î °ñ°í·ç Àß ³ª¿Ô´ÂÁö Á¡°ËÇÑ´Ù.
¿¹ Á¦ : count2 |
#include <Turboc.h>
#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));
}
}
NUM Å©±âÀÇ º¤Å͸¦ ¼±¾ðÇϰí for_each¹®À¸·Î º¤ÅÍÀÇ °¢ ¿ä¼Ò¿¡ RANGE ¹Ì¸¸ÀÇ ³¼ö¸¦ »ý¼ºÇÏ¿© ä¿ö ³Ö¾ú´Ù. for_each°¡ ·¹ÆÛ·±½º¸¦ Àü´Þ¹ÞÀ¸¸é ¿ä¼Ò°ªÀ» º¯°æÇÒ ¼öµµ ÀÖ´Ù. º¤Å͸¦ ÃʱâÈÇÑ ÈÄ ³¼öµéÀÌ ¸î °³¾¿ »ý¼ºµÇ¾ú´ÂÁö count ÇÔ¼ö·Î ¼¼¾î º¸¾Ò´Ù. ½ÇÇà °á°ú´Â ´ÙÀ½°ú °°´Ù.
00ÀÇ ÃâÇö ȸ¼ö : 215
01ÀÇ ÃâÇö ȸ¼ö : 200
02ÀÇ ÃâÇö ȸ¼ö : 193
03ÀÇ ÃâÇö ȸ¼ö : 183
04ÀÇ ÃâÇö ȸ¼ö : 205
05ÀÇ ÃâÇö ȸ¼ö : 207
06ÀÇ ÃâÇö ȸ¼ö : 191
07ÀÇ ÃâÇö ȸ¼ö : 214
08ÀÇ ÃâÇö ȸ¼ö : 191
09ÀÇ ÃâÇö ȸ¼ö : 201
´ëü·Î 200 ÀüÈÄÀÇ È¸¼ö·Î »ý¼ºµÇ¾ú´Âµ¥ °ñ°í·ç ³¼öµéÀÌ Àß »ý¼ºµÇ¾úÀ½À» È®ÀÎÇÒ ¼ö ÀÖ´Ù. °¢°¢ÀÇ ¼ö¿¡ ´ëÇÑ ÃâÇö ºóµµ¸¦ ÇÔ¼ö È£Ãâ Çϳª·Î ±¸ÇÒ ¼ö ÀÖ´Ù´Â ¸é¿¡¼ °£ÆíÇϱâ´Â ÇÏÁö¸¸ »ç½Ç»óÀÇ ÀÌÁß ·çÇÁ¶ó C·Î ¹®Á¦¸¦ Á÷Á¢ Ǫ´Â °Íº¸´Ù ¼º´ÉÀº ´Ù¼Ò ´À¸®´Ù.