STL_算法_元素计数(count、count_if)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了STL_算法_元素计数(count、count_if),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4348字,纯文字阅读大概需要7分钟。
内容图文
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; }
原文:http://blog.csdn.net/u010579068/article/details/50388317
内容总结
以上是互联网集市为您收集整理的STL_算法_元素计数(count、count_if)全部内容,希望文章能够帮你解决STL_算法_元素计数(count、count_if)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。