diff --git a/TinySTL/Algorithm.h b/TinySTL/Algorithm.h new file mode 100644 index 0000000..2bb7d18 --- /dev/null +++ b/TinySTL/Algorithm.h @@ -0,0 +1,44 @@ +#ifndef _ALGORITHM_H_ +#define _ALGORITHM_H_ + +#include + +namespace TinySTL{ + //********* fill ******************** + template + 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(value), last - first); + } + inline void fill(wchar_t *first, wchar_t *last, const wchar_t& value) + { + memset(first, static_cast(value), (last - first) * sizeof(wchar_t)); + } + //********* fill_n ******************** + template + OutputIterator fill_n(OutputIterator first, Size n, const T& value) + { + for (; n > 0; --n, ++first) + *first = value; + return first; + } + template + void *fill_n(char *first, Size n, const char& value) + { + memset(first, static_cast(value), n); + return first + n; + } + template + void *fill_n(wchar_t *first, Size n, const wchar_t& value) + { + memset(first, static_cast(value), n * sizeof(wchar_t)); + return first + n; + } +} + +#endif \ No newline at end of file diff --git a/TinySTL/TinySTL.vcxproj b/TinySTL/TinySTL.vcxproj index 1932559..3000608 100644 --- a/TinySTL/TinySTL.vcxproj +++ b/TinySTL/TinySTL.vcxproj @@ -79,6 +79,7 @@ + diff --git a/TinySTL/TinySTL.vcxproj.filters b/TinySTL/TinySTL.vcxproj.filters index d4980ba..c6fe811 100644 --- a/TinySTL/TinySTL.vcxproj.filters +++ b/TinySTL/TinySTL.vcxproj.filters @@ -41,5 +41,8 @@ 头文件 + + 头文件 + \ No newline at end of file diff --git a/TinySTL/UninitializedFunctions.h b/TinySTL/UninitializedFunctions.h index 1bb4d5c..4c8149c 100644 --- a/TinySTL/UninitializedFunctions.h +++ b/TinySTL/UninitializedFunctions.h @@ -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 void _uninitialized_fill_aux(ForwardIterator first, ForwardIterator last, const T& value, _true_type){ - //TODO - //»»³É×Ô¼ºµÄfillº¯Êý - std::fill(first, last, value); + fill(first, last, value); } template void _uninitialized_fill_aux(ForwardIterator first, ForwardIterator last, @@ -82,9 +81,7 @@ namespace TinySTL{ template ForwardIterator _uninitialized_n_fill_aux(ForwardIterator first, Size n, const T& x, _true_type){ - //TODO - //»»³É×Ô¼ºµÄfill_nº¯Êý - return std::fill_n(first, n, x); + return fill_n(first, n, x); } template ForwardIterator _uninitialized_n_fill_aux(ForwardIterator first, diff --git a/TinySTL/main.cpp b/TinySTL/main.cpp index d3c30ef..e138036 100644 --- a/TinySTL/main.cpp +++ b/TinySTL/main.cpp @@ -15,23 +15,8 @@ int main(){ int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; //TinySTL::vector svec(array, array + 5); //svec.erase(svec.begin() + 1, svec.begin() + 4);//1,5 - TinySTL::vector 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 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");