count_if 源码剖析
来源:互联网 发布:磁力链接软件 编辑:程序博客网 时间:2024/05/30 12:03
一:用法解析
函数原型:template <class InputIterator, class UnaryPredicate>
typename iterator_traits<InputIterator>::difference_type
count_if (InputIterator first, InputIterator last, UnaryPredicate pred);
不理解typename的含义,可以参考http://blog.csdn.net/laojiu_/article/details/51737114
功能:
在区间[ first , last )找到使pred为真的数,返回其个数。
例子:
// count_if example#include <iostream> // std::cout#include <algorithm> // std::count_if#include <vector> // std::vectorbool IsOdd (int i) { return ((i%2)==1); }int main () { std::vector<int> myvector; for (int i=1; i<10; i++) myvector.push_back(i); // myvector: 1 2 3 4 5 6 7 8 9 int mycount = count_if (myvector.begin(), myvector.end(), IsOdd); std::cout << "myvector contains " << mycount << " odd values.\n"; return 0;}运行如下:
myvector contains 5 odd values.
二:源码剖析
// TEMPLATE FUNCTION count_iftemplate<class _InIt,class _Pr> inlinetypename iterator_traits<_InIt>::difference_type_Count_if(_InIt _First, _InIt _Last, _Pr _Pred){// count elements satisfying _Predtypename iterator_traits<_InIt>::difference_type _Count = 0;for (; _First != _Last; ++_First)if (_Pred(*_First))++_Count;return (_Count);}template<class _InIt,class _Pr> inlinetypename iterator_traits<_InIt>::difference_typecount_if(_InIt _First, _InIt _Last, _Pr _Pred){// count elements satisfying _Pred_DEBUG_RANGE_PTR(_First, _Last, _Pred);return (_Count_if(_Unchecked(_First), _Unchecked(_Last), _Pred));}
源码摘抄自Visual Studio 2015安装目录algorithm文件中。
点击进入目录----> C++源码剖析目录
0 0
- count_if 源码剖析
- count_if
- count_if
- count_if
- count_if
- 《stl源码剖析》剖析
- count_if用法
- count_if详解
- count,count_if
- count count_if
- 【源码】ArrayList源码剖析
- 【源码】LinkedList源码剖析
- 【源码】HashMap源码剖析
- 【源码】HashMap源码剖析
- 【源码】Hashtable源码剖析
- 【源码】LinkedHashMap源码剖析
- 【源码】LruCache源码剖析
- 【源码】TreeMap源码剖析
- liunx apache 的安装
- 为Grub启动菜单添加Windows选项
- 英文歌曲:Iridescent (变形金刚第三部主题曲)
- The VMware Authorization Service is not running错误的处理方式
- 跟我一起写Makefile(15)--使用make更新函数库文件(函数库文件的成员+函数库成员的隐含规则+函数库文件的后缀规则)
- count_if 源码剖析
- 严重: Error configuring application listener of class org.springframework.web.context.ContextLoaderLis
- 每日一练——大数加减乘除运算实现(网易笔试题)
- 多线程编程入门(17):线程同步工具之CountDownLatch
- hdu 5806 NanoApe Loves Sequence Ⅱ
- 数学在机器学习中的重要性
- 占位
- BestCoder Round #86
- Unity学习笔记 4th —— 现在让我们来学一学材质制作