现在的位置: 首页 > 综合 > 正文

count()与count_if()的使用示例

2013年03月08日 ⁄ 综合 ⁄ 共 611字 ⁄ 字号 评论关闭

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

int main(int argc,_TCHAR* argv[])
{
 string a;
 vector<string> v;
 for (int i = 0;i < 5;i++)
 {
  cin>>a;
  v.push_back(a);
 }
 string target_value = "oyh";
 int num_items = count(v.begin(),v.end(),target_value);
 cout<<num_items<<endl;

 int nums[] = { 3, 1, 2, 3, 4, 3, 9, 3, 13 };
 int start = 0;
 int end = 9;

 int target_value = 3;
 int num_items = count_if( nums+start,
  nums+end,
  bind2nd(greater<int>(), target_value) );
 int num_items2 = count_if( nums+start,
  nums+end,
  bind2nd(equal_to<int>(), target_value) );

 cout <<num_items<< "/t"<< num_items2 << endl;

 return 0;
}

抱歉!评论已关闭.