添加fill和fill_n算法

This commit is contained in:
邹晓航
2014-09-22 15:19:04 +08:00
parent e280b2eb18
commit 9a1ee1e7c7
5 changed files with 53 additions and 23 deletions

44
TinySTL/Algorithm.h Normal file
View File

@@ -0,0 +1,44 @@
#ifndef _ALGORITHM_H_
#define _ALGORITHM_H_
#include <cstring>
namespace TinySTL{
//********* fill ********************
template<class ForwardIterator, class T>
void fill(ForwardIterator first, ForwardIterator last, const T& value)
{
for (; first != last; ++first)
*first = value;
}
inline void fill(char *first, char *last, const char& value)
{
memset(first, static_cast<unsigned char>(value), last - first);
}
inline void fill(wchar_t *first, wchar_t *last, const wchar_t& value)
{
memset(first, static_cast<unsigned char>(value), (last - first) * sizeof(wchar_t));
}
//********* fill_n ********************
template<class OutputIterator, class Size, class T>
OutputIterator fill_n(OutputIterator first, Size n, const T& value)
{
for (; n > 0; --n, ++first)
*first = value;
return first;
}
template<class Size>
void *fill_n(char *first, Size n, const char& value)
{
memset(first, static_cast<unsigned char>(value), n);
return first + n;
}
template<class Size>
void *fill_n(wchar_t *first, Size n, const wchar_t& value)
{
memset(first, static_cast<unsigned char>(value), n * sizeof(wchar_t));
return first + n;
}
}
#endif