STL_算法_元素計數(count、count_if) -开发者知识库

STL_算法_元素計數(count、count_if) -开发者知识库,第1张

 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条回复