添加fill和fill_n算法
This commit is contained in:
44
TinySTL/Algorithm.h
Normal file
44
TinySTL/Algorithm.h
Normal 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
|
||||
Reference in New Issue
Block a user