From d4eab62cacdb94506e1811ee27f23d6f2510b6ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E6=99=93=E8=88=AA?= <1210603696@qq.com> Date: Fri, 31 Oct 2014 14:11:07 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90count=5Fif?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TinySTL/Algorithm.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/TinySTL/Algorithm.h b/TinySTL/Algorithm.h index af05fdc..1007f85 100644 --- a/TinySTL/Algorithm.h +++ b/TinySTL/Algorithm.h @@ -333,6 +333,18 @@ namespace TinySTL{ } return n; } + //********** [count_if] ****************************** + //********* [Algorithm Complexity: O(N)] **************** + template + typename iterator_traits::difference_type + count_if(InputIterator first, InputIterator last, UnaryPredicate pred){ + typename iterator_traits::difference_type n = 0; + for (; first != last; ++first){ + if (pred(*first)) + ++n; + } + return n; + } }