From 39822ddfbe18dca50dec21c6ed7991beffa2ce29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E6=99=93=E8=88=AA?= <1210603696@qq.com> Date: Fri, 19 Sep 2014 12:30:23 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=93=8D=E4=BD=9C=E5=86=85?= =?UTF-8?q?=E5=AD=98=E7=9A=84=E5=B7=A5=E5=85=B7=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TinySTL/UninitializedFunctions.h | 102 +++++++++++++++++++++++++++++++ TinySTL/main.cpp | 2 +- 2 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 TinySTL/UninitializedFunctions.h diff --git a/TinySTL/UninitializedFunctions.h b/TinySTL/UninitializedFunctions.h new file mode 100644 index 0000000..8869b1e --- /dev/null +++ b/TinySTL/UninitializedFunctions.h @@ -0,0 +1,102 @@ +#ifndef _UNINITIALIZED_FUNCTIONS_H_ +#define _UNINITIALIZED_FUNCTIONS_H_ + +#include "Construct.h" +#include "Iterator.h" +#include "TypeTraits.h" + +#include + +namespace TinySTL{ + + /***************************************************************************/ + template + ForwardIterator _uninitialized_copy_aux(InputIterator first, InputIterator last, + ForwardIterator result, _true_type); + template + ForwardIterator _uninitialized_copy_aux(InputIterator first, InputIterator last, + ForwardIterator result, _false_type); + + template + ForwardIterator uninitialized_copy(InputIterator first, InputIterator last, ForwardIterator result){ + typedef typename _type_traits::value_type>::is_POD_type isPODType; + return _uninitialized_copy_aux(first, last, result, isPODType()); + } + template + ForwardIterator _uninitialized_copy_aux(InputIterator first, InputIterator last, + ForwardIterator result, _true_type){ + memcpy(result, first, (last - first) * sizeof(*first)); + return result + (last - first); + } + template + ForwardIterator _uninitialized_copy_aux(InputIterator first, InputIterator last, + ForwardIterator result, _false_type){ + int i = 0; + for (; first != last; ++first, ++i){ + construct((result + i), *first); + } + return (result + i); + } + + /***************************************************************************/ + template + void _uninitialized_fill_aux(ForwardIterator first, ForwardIterator last, + const T& value, _true_type); + template + void _uninitialized_fill_aux(ForwardIterator first, ForwardIterator last, + const T& value, _false_type); + + template + void uninitialized_fill(ForwardIterator first, ForwardIterator last, const T& value){ + typedef typename _type_traits::is_POD_type isPODType; + _uninitialized_fill_aux(first, last, value, isPODType()); + } + template + void _uninitialized_fill_aux(ForwardIterator first, ForwardIterator last, + const T& value, _true_type){ + //TODO + //换成自己的fill函数 + std::fill(first, last, value); + } + template + void _uninitialized_fill_aux(ForwardIterator first, ForwardIterator last, + const T& value, _false_type){ + int i = 0; + for (; first != last; ++first, ++i){ + construct(first, value); + } + } + + /***************************************************************************/ + template + ForwardIterator _uninitialized_n_fill_aux(ForwardIterator first, + Size n, const T& x, _true_type); + template + ForwardIterator _uninitialized_n_fill_aux(ForwardIterator first, + Size n, const T& x, _false_type); + + template + inline ForwardIterator uninitialized_fill_n(ForwardIterator first, + Size n, const T& x){ + typedef typename _type_traits::is_POD_type isPODType; + return _uninitialized_n_fill_aux(first, n, x, isPODType()); + } + 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); + } + template + ForwardIterator _uninitialized_n_fill_aux(ForwardIterator first, + Size n, const T& x, _false_type){ + int i = 0; + for (; i != n;){ + construct((first + i), x); + } + return (first + i); + } +} + +#endif \ No newline at end of file diff --git a/TinySTL/main.cpp b/TinySTL/main.cpp index d56e9f6..224c42f 100644 --- a/TinySTL/main.cpp +++ b/TinySTL/main.cpp @@ -17,7 +17,7 @@ int main(){ auto last = p + 100; int array[100]; TinySTL::uninitialized_fill_n(array, 100, 88); - for (auto n : array){ cout << n << endl; } + for (auto n : array){ cout << n << endl; } system("pause"); return 0; } \ No newline at end of file