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

 C++ Primer 学习中。。。

 

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

 

count 、 count_if

 

 

#include
#include
#include
#include
#include
#include
using namespace std;

/***********************
count
count_if
关联容器的等效成员函数
    set.count
    multiset.count
    map.count
    multimap.count
************************/
/***********************  std::count:****************************************
template 
  typename iterator_traits::difference_type
    count ( ForwardIterator first, ForwardIterator last, const T& value );
//eg:
template 
  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 
  typename iterator_traits::difference_type
    count_if ( ForwardIterator first, ForwardIterator last, Predicate pred );
//eg:
template 
  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 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 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(),2));//表示param1 % 2
    cout << "myvector contains " << mycount  << " odd values.\n";//奇数
//  如果求偶数的个数                                            not1,1表示一个参数取反
    mycount = (int) count_if (myvector.begin(), myvector.end(), not1(bind2nd(modulus(),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(),4));
    cout<<"有"< param2
    mycount=count_if(myvector.begin(),myvector.end(),bind1st(greater(),4));
    cout<<"有"<(),4));
    cout<<"有"<= 4
    mycount=count_if(myvector.begin(),myvector.end(),bind2nd(greater_equal(),4));
    cout<<"有"<(),4));
    cout<<"有"< 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::iterator ims=ms.begin();
    while(ims!=ms.end()){
        cout<<*ims++<<" ";
    }cout<

 

你可能感兴趣的:(STL_算法,c++,prime,程序员面试金典)