STL_算法_元素计数(count、count_if)

 C++ Primer 学习中。。

 

简单记录下我的学习过程 (代码为主)

 

count 、 count_if

 

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<set>
#include<algorithm>
using namespace std;

/***********************
count
count_if
关联容器的等效成员函数
    set.count
    multiset.count
    map.count
    multimap.count
************************/
/***********************  std::count:****************************************
template <class InputIterator, class T>
  typename iterator_traits<InputIterator>::difference_type
    count ( ForwardIterator first, ForwardIterator last, const T& value );
//eg:
template <class InputIterator, class T>
  ptrdiff_t count ( InputIterator first, InputIterator last, const T& value )
{
  ptrdiff_t ret=0;
  while (first != last) if (*first++ == value) ++ret;
  return ret;
}
*******************************************************************************/

/***********************  std::count_if:****************************************
template <class InputIterator, class Predicate>
  typename iterator_traits<InputIterator>::difference_type
    count_if ( ForwardIterator first, ForwardIterator last, Predicate pred );
//eg:
template <class InputIterator, class Predicate>
  ptrdiff_t count_if ( InputIterator first, InputIterator last, Predicate pred )//pred 为函数or函数对象
{
  ptrdiff_t ret=0;
  while (first != last) if (pred(*first++)) ++ret;
  return ret;
}
*******************************************************************************/

//奇数
bool IsOdd (int i)
{
    return i&1;
}

int main()
{
    int mycount;

    // counting elements in array:
    int myints[] = {10,20,30,30,20,10,10,20};   // 8 elements
    mycount = (int) count (myints, myints+8, 10);
    cout << "10 appears " << mycount << " times.\n";

    // counting elements in container:
    vector<int> myvector (myints, myints+8);
    mycount = (int) count (myvector.begin(), myvector.end(), 20);//有几个20
    cout << "20 appears " << mycount  << " times.\n";

    /****************
    Output:
    10 appears 3 times.
    20 appears 3 times.
    ****************/

//    vector<int> myvector;
    myvector.clear();
    for (int i=1; i<10; i++) myvector.push_back(i); // myvector: 1 2 3 4 5 6 7 8 9

    cout<<"\nmyvector: 1 2 3 4 5 6 7 8 9 \n";
//    mycount = (int) count_if (myvector.begin(), myvector.end(), IsOdd);
    mycount = (int) count_if (myvector.begin(), myvector.end(), bind2nd(modulus<int>(),2));//表示param1 % 2
    cout << "myvector contains " << mycount  << " odd values.\n";//奇数
//  假设求偶数的个数                                            not1,1表示一个參数取反
    mycount = (int) count_if (myvector.begin(), myvector.end(), not1(bind2nd(modulus<int>(),2)));//表示!(param1 % 2)
    cout << "myvector contains " << mycount  << " even values.\n";//偶数
    /****************
    Output:
    myvector contains 5 odd values.
    ****************/
//                                               函数适配器  函数对象
//         bind2nd(op,value);表示绑定第二个数                   param1 > 4  这里表示统计大于4的个数
    mycount=count_if(myvector.begin(),myvector.end(),bind2nd(greater<int>(),4));
    cout<<"有"<<mycount<<"个数大于4"<<endl;
//拓展练习                                                      4 > param2
    mycount=count_if(myvector.begin(),myvector.end(),bind1st(greater<int>(),4));
    cout<<"有"<<mycount<<"个数小于4"<<endl;
//                                                              4 < param2
    mycount=count_if(myvector.begin(),myvector.end(),bind1st(less<int>(),4));
    cout<<"有"<<mycount<<"个数大于4"<<endl;
//                                                              param1 >= 4
    mycount=count_if(myvector.begin(),myvector.end(),bind2nd(greater_equal<int>(),4));
    cout<<"有"<<mycount<<"个数大于等于4"<<endl;
//                                                              param1 <= 4
    mycount=count_if(myvector.begin(),myvector.end(),bind2nd(less_equal<int>(),4));
    cout<<"有"<<mycount<<"个数小于等于4"<<endl;

/****关联容器****/
    multiset<int> ms(myvector.begin(),myvector.end());
    ms.insert(myvector.begin(),myvector.begin()+6);
    ms.insert(myvector.begin(),myvector.begin()+4);
    ms.insert(myvector.begin(),myvector.begin()+2);
    ms.insert(1);

    multiset<int>::iterator ims=ms.begin();
    while(ims!=ms.end()){
        cout<<*ims++<<" ";
    }cout<<endl;

//两种方法求1的个数
    int cnt=count(ms.begin(),ms.end(),1);//全部容器适用可是比較慢些
    cout<<"multiset里有"<<cnt<<"个1."<<endl;

    cnt=ms.count(1);//关联容器专享   set已经排序能够高速计数
    cout<<"multiset里有"<<cnt<<"个1."<<endl;

    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值