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.
";

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

    /****************
    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<<"
myvector: 1 2 3 4 5 6 7 8 9 
";
//    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.
";//奇数
//  假设求偶数的个数                                            not1,1表示一个參数取反
    mycount = (int) count_if (myvector.begin(), myvector.end(), not1(bind2nd(modulus<int>(),2)));//表示!(param1 % 2)
    cout << "myvector contains " << mycount  << " even values.
";//偶数
    /****************
    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;
}
原文地址:https://www.cnblogs.com/wzzkaifa/p/7145313.html