添加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
|
||||
@@ -79,6 +79,7 @@
|
||||
<ClCompile Include="main.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="Algorithm.h" />
|
||||
<ClInclude Include="Alloc.h" />
|
||||
<ClInclude Include="Allocator.h" />
|
||||
<ClInclude Include="Construct.h" />
|
||||
|
||||
@@ -41,5 +41,8 @@
|
||||
<ClInclude Include="Vector.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Algorithm.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -1,6 +1,7 @@
|
||||
#ifndef _UNINITIALIZED_FUNCTIONS_H_
|
||||
#define _UNINITIALIZED_FUNCTIONS_H_
|
||||
|
||||
#include "Algorithm.h"
|
||||
#include "Construct.h"
|
||||
#include "Iterator.h"
|
||||
#include "TypeTraits.h"
|
||||
@@ -52,9 +53,7 @@ namespace TinySTL{
|
||||
template<class ForwardIterator, class T>
|
||||
void _uninitialized_fill_aux(ForwardIterator first, ForwardIterator last,
|
||||
const T& value, _true_type){
|
||||
//TODO
|
||||
//<2F><><EFBFBD><EFBFBD><EFBFBD>Լ<EFBFBD><D4BC><EFBFBD>fill<6C><6C><EFBFBD><EFBFBD>
|
||||
std::fill(first, last, value);
|
||||
fill(first, last, value);
|
||||
}
|
||||
template<class ForwardIterator, class T>
|
||||
void _uninitialized_fill_aux(ForwardIterator first, ForwardIterator last,
|
||||
@@ -82,9 +81,7 @@ namespace TinySTL{
|
||||
template<class ForwardIterator, class Size, class T>
|
||||
ForwardIterator _uninitialized_n_fill_aux(ForwardIterator first,
|
||||
Size n, const T& x, _true_type){
|
||||
//TODO
|
||||
//<2F><><EFBFBD><EFBFBD><EFBFBD>Լ<EFBFBD><D4BC><EFBFBD>fill_n<5F><6E><EFBFBD><EFBFBD>
|
||||
return std::fill_n(first, n, x);
|
||||
return fill_n(first, n, x);
|
||||
}
|
||||
template<class ForwardIterator, class Size, class T>
|
||||
ForwardIterator _uninitialized_n_fill_aux(ForwardIterator first,
|
||||
|
||||
@@ -15,23 +15,8 @@ int main(){
|
||||
int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
|
||||
//TinySTL::vector<int> svec(array, array + 5);
|
||||
//svec.erase(svec.begin() + 1, svec.begin() + 4);//1,5
|
||||
TinySTL::vector<int> svec;
|
||||
cout << svec.capacity() << endl;
|
||||
svec.push_back(1);
|
||||
cout << svec.capacity() << endl;
|
||||
svec.push_back(2);
|
||||
cout << svec.capacity() << endl;
|
||||
svec.push_back(3);
|
||||
cout << svec.capacity() << endl;
|
||||
svec.push_back(4);
|
||||
cout << svec.capacity() << endl;
|
||||
svec.push_back(5);
|
||||
cout << svec.capacity() << endl;
|
||||
svec.push_back(6);
|
||||
cout << svec.capacity() << endl;
|
||||
svec.push_back(7);
|
||||
cout << svec.capacity() << endl;
|
||||
//svec.insert(svec.begin() + 0, array, array + 10);
|
||||
TinySTL::vector<int> svec(10, 0);
|
||||
svec.insert(svec.begin() + 0, array, array + 10);
|
||||
//svec.insert(svec.begin()+0, 10, 99);
|
||||
for (auto s : svec){ cout << s << endl; }
|
||||
system("pause");
|
||||
|
||||
Reference in New Issue
Block a user