From 2fb1fbaf68df1bf6c86cbc22fb88682a25b33651 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:29:38 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0Iterator=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E8=90=83=E5=8F=96=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TinySTL/Iterator.h | 110 ++++++++++++++++++++++++++++++++ TinySTL/TinySTL.vcxproj | 1 + TinySTL/TinySTL.vcxproj.filters | 3 + TinySTL/main.cpp | 15 +++-- 4 files changed, 124 insertions(+), 5 deletions(-) create mode 100644 TinySTL/Iterator.h diff --git a/TinySTL/Iterator.h b/TinySTL/Iterator.h new file mode 100644 index 0000000..d18e628 --- /dev/null +++ b/TinySTL/Iterator.h @@ -0,0 +1,110 @@ +#ifndef _ITERATOR_H_ +#define _ITERATOR_H_ + +namespace TinySTL{ + + struct input_iterator_tag{}; + struct output_iterator_tag{}; + struct forward_iterator_tag :public input_iterator_tag {}; + struct bidirectional_iterator_tag :public forward_iterator_tag {}; + struct random_access_iterator_tag :public bidirectional_iterator_tag {}; + + template struct input_iterator + { + typedef input_iterator_tag iterator_category; + typedef T value_type; + typedef Distance difference_type; + typedef T* pointer; + typedef T& reference; + }; + struct output_iterator + { + typedef output_iterator_tag iterator_category; + typedef void value_type; + typedef void difference_type; + typedef void pointer; + typedef void reference; + }; + template struct forward_iterator + { + typedef forward_iterator_tag iterator_category; + typedef T value_type; + typedef Distance difference_type; + typedef T* pointer; + typedef T& reference; + }; + template struct bidirectional_iterator + { + typedef bidirectional_iterator_tag iterator_category; + typedef T value_type; + typedef Distance difference_type; + typedef T* pointer; + typedef T& reference; + }; + template struct random_access_iterator + { + typedef random_access_iterator_tag iterator_category; + typedef T value_type; + typedef Distance difference_type; + typedef T* pointer; + typedef T& reference; + }; + + template + struct iterator + { + typedef Category iterator_category; + typedef T value_type; + typedef Distance difference_type; + typedef Pointer pointer; + typedef Reference reference; + }; + + template + struct iterator_traits + { + typedef typename Iterator::Category iterator_category; + typedef typename Iterator::value_type value_type; + typedef typename Iterator::Distance difference_type; + typedef typename Iterator::Pointer pointer; + typedef typename Iterator::Reference reference; + }; + template + struct iterator_traits + { + typedef random_access_iterator_tag iterator_category; + typedef T value_type; + typedef ptrdiff_t difference_type; + typedef T* pointer; + typedef T& reference; + }; + template + struct iterator_traits + { + typedef random_access_iterator_tag iterator_category; + typedef T value_type; + typedef ptrdiff_t difference_type; + typedef const T* pointer; + typedef const T& reference; + }; + + template + inline typename iterator_traits::iterator_category + iterator_category(const Iterator& It){ + typedef typename iterator_traits::iterator_category category; + return category(); + } + template + inline typename iterator_traits::value_type + value_type(const Iterator& It){ + return static_cast::value_type*>(0); + } + template + inline typename iterator_traits::difference_type + difference_type(const Iterator& It){ + return static_cast::difference_type*>(0); + } +} + +#endif \ No newline at end of file diff --git a/TinySTL/TinySTL.vcxproj b/TinySTL/TinySTL.vcxproj index 39e827c..e2ac101 100644 --- a/TinySTL/TinySTL.vcxproj +++ b/TinySTL/TinySTL.vcxproj @@ -82,6 +82,7 @@ + diff --git a/TinySTL/TinySTL.vcxproj.filters b/TinySTL/TinySTL.vcxproj.filters index 657d1f1..4838c9a 100644 --- a/TinySTL/TinySTL.vcxproj.filters +++ b/TinySTL/TinySTL.vcxproj.filters @@ -35,5 +35,8 @@ 头文件 + + 头文件 + \ No newline at end of file diff --git a/TinySTL/main.cpp b/TinySTL/main.cpp index 96da56c..d56e9f6 100644 --- a/TinySTL/main.cpp +++ b/TinySTL/main.cpp @@ -3,16 +3,21 @@ #include "Allocator.h" #include "Construct.h" +#include "UninitializedFunctions.h" using namespace std; int main(){ - for (int i = 1; i != 100000; ++i){ + /*for (int i = 1; i != 100000; ++i){ auto p = TinySTL::allocator::allocate(); - TinySTL::allocator::construct(p, i); - TinySTL::allocator::destroy(p); - TinySTL::allocator::deallocate(p); - } + + }*/ + auto p = TinySTL::allocator::allocate(100); + TinySTL::uninitialized_fill(p, p + 100, 88); + auto last = p + 100; + int array[100]; + TinySTL::uninitialized_fill_n(array, 100, 88); + for (auto n : array){ cout << n << endl; } system("pause"); return 0; } \ No newline at end of file