From 2016eb67eaf40142cac8f052b1eeb20545fa61e8 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:08:39 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90count?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TinySTL/Algorithm.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/TinySTL/Algorithm.h b/TinySTL/Algorithm.h index e43717a..af05fdc 100644 --- a/TinySTL/Algorithm.h +++ b/TinySTL/Algorithm.h @@ -1,7 +1,9 @@ #ifndef _ALGORITHM_H_ #define _ALGORITHM_H_ +#include "Allocator.h" #include "Functional.h" +#include "Iterator.h" #include "TypeTraits.h" #include @@ -319,6 +321,18 @@ namespace TinySTL{ } return first; } + //********** [count] ****************************** + //********* [Algorithm Complexity: O(N)] **************** + template + typename iterator_traits::difference_type + count(InputIterator first, InputIterator last, const T& val){ + typename iterator_traits::difference_type n = 0; + for (; first != last; ++first){ + if (*first == val) + ++n; + } + return n; + } }