添加Iterator类型萃取功能

This commit is contained in:
邹晓航
2014-09-19 12:29:38 +08:00
parent 0313b51ffb
commit 2fb1fbaf68
4 changed files with 124 additions and 5 deletions

110
TinySTL/Iterator.h Normal file
View File

@@ -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 <class T, class Distance> 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 <class T, class Distance> struct forward_iterator
{
typedef forward_iterator_tag iterator_category;
typedef T value_type;
typedef Distance difference_type;
typedef T* pointer;
typedef T& reference;
};
template <class T, class Distance> struct bidirectional_iterator
{
typedef bidirectional_iterator_tag iterator_category;
typedef T value_type;
typedef Distance difference_type;
typedef T* pointer;
typedef T& reference;
};
template <class T, class Distance> 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<class Category, class T, class Distance = ptrdiff_t,
class Pointer = T*, class Reference = T&>
struct iterator
{
typedef Category iterator_category;
typedef T value_type;
typedef Distance difference_type;
typedef Pointer pointer;
typedef Reference reference;
};
template<class Iterator>
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<class T>
struct iterator_traits<T*>
{
typedef random_access_iterator_tag iterator_category;
typedef T value_type;
typedef ptrdiff_t difference_type;
typedef T* pointer;
typedef T& reference;
};
template<class T>
struct iterator_traits<const T*>
{
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<class Iterator>
inline typename iterator_traits<Iterator>::iterator_category
iterator_category(const Iterator& It){
typedef typename iterator_traits<Iterator>::iterator_category category;
return category();
}
template<class Iterator>
inline typename iterator_traits<Iterator>::value_type
value_type(const Iterator& It){
return static_cast<typename iterator_traits<Iterator>::value_type*>(0);
}
template<class Iterator>
inline typename iterator_traits<Iterator>::difference_type
difference_type(const Iterator& It){
return static_cast<typename iterator_traits<Iterator>::difference_type*>(0);
}
}
#endif

View File

@@ -82,6 +82,7 @@
<ClInclude Include="Alloc.h" />
<ClInclude Include="Allocator.h" />
<ClInclude Include="Construct.h" />
<ClInclude Include="Iterator.h" />
<ClInclude Include="TypeTraits.h" />
<ClInclude Include="UninitializedFunctions.h" />
</ItemGroup>

View File

@@ -35,5 +35,8 @@
<ClInclude Include="UninitializedFunctions.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="Iterator.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@@ -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<int>::allocate();
TinySTL::allocator<int>::construct(p, i);
TinySTL::allocator<int>::destroy(p);
TinySTL::allocator<int>::deallocate(p);
}
}*/
auto p = TinySTL::allocator<int>::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;
}