完成count

This commit is contained in:
邹晓航
2014-10-31 14:08:39 +08:00
parent 24ba5808d4
commit 2016eb67ea

View File

@@ -1,7 +1,9 @@
#ifndef _ALGORITHM_H_
#define _ALGORITHM_H_
#include "Allocator.h"
#include "Functional.h"
#include "Iterator.h"
#include "TypeTraits.h"
#include <cstring>
@@ -319,6 +321,18 @@ namespace TinySTL{
}
return first;
}
//********** [count] ******************************
//********* [Algorithm Complexity: O(N)] ****************
template <class InputIterator, class T>
typename iterator_traits<InputIterator>::difference_type
count(InputIterator first, InputIterator last, const T& val){
typename iterator_traits<InputIterator>::difference_type n = 0;
for (; first != last; ++first){
if (*first == val)
++n;
}
return n;
}
}