1. 程式人生 > >迭代器概念與Traits程式設計技法

迭代器概念與Traits程式設計技法

 1 #include <iostream>
 2 using namespace std;
 3 
 4 template <class T>
 5 struct MyIter
 6 {
 7     typedef T value_type;
 8     T* ptr;
 9     MyIter(T *p = 0) : ptr(p) {}
10     T& operator*() const { return *ptr; }
11 };
12 
13 template <class I>
14 typename I::value_type func(I ite)
15 { 16 return *ite; 17 } 18 19 int main() 20 { 21 MyIter<int> iter1(new int(8)); 22 cout << func(iter1) << " "; 23 MyIter<char> iter2(new char('a')); 24 cout << func(iter2) << " "; 25 system("pause"); 26 return 0; 27 }

輸出結果:

 

  1 /* https://blog.csdn.net/damotiansheng/article/details/41379151 */
  2 
  3 #include <iostream>
  4 #include <new.h>
  5 //using namespace std; //不要std,因為std名稱空間中也有input_iterator_tag等的定義
  6  
  7 struct input_iterator_tag {};   //std名稱空間中也有input_iterator_tag的定義
  8 struct output_iterator_tag {};
9 struct forward_iterator_tag: public input_iterator_tag {}; 10 struct bidirectional_iterator_tag: public forward_iterator_tag {}; 11 struct random_access_iterator_tag: public bidirectional_iterator_tag {}; 12 13 struct __true_type {}; 14 struct __false_type {}; 15 16 template <class T1, class T2> 17 inline void construct( T1 *p, const T2& value ) 18 { 19 new (p) T1(value); 20 return; 21 } 22 23 24 template <class OutputIterator, class Size, class T> 25 OutputIterator fill_n( OutputIterator first, Size n, const T& value) 26 { 27 for( ; n > 0; --n, ++first) 28 { 29 *first = value; 30 } 31 return first; 32 } 33 34 template <class Category, class T, class Distance = ptrdiff_t, class Pointer = T*, class Reference = T&> 35 struct iterator 36 { 37 typedef Category iterator_category; 38 typedef T value_type; 39 typedef Distance difference_type; 40 typedef Pointer pointer; 41 typedef Reference reference; 42 }; 43 44 template <class Iterator> 45 struct iterator_traits // iterator_traits負責萃取迭代器的特性,例如獲得迭代器指向元素的型別等 46 { 47 typedef typename Iterator::iterator_category iterator_category; 48 typedef typename Iterator::value_type value_type; 49 typedef typename Iterator::difference_type difference_type; 50 typedef typename Iterator::pointer pointer; 51 typedef typename Iterator::reference reference; 52 }; 53 54 template <class T> 55 struct iterator_traits<T*> 56 { 57 typedef random_access_iterator_tag iterator_category; 58 typedef T value_type; 59 typedef ptrdiff_t difference_type; 60 typedef T* pointer; 61 typedef T& reference; 62 }; 63 64 template <class Iterator> 65 inline typename iterator_traits<Iterator>::value_type* value_type( const Iterator&) 66 { 67 return static_cast<typename iterator_traits<Iterator>::value_type*>(0); 68 } 69 70 template <class type> 71 struct __type_traits 72 { 73 typedef __false_type has_trivial_default_constructor; 74 typedef __false_type has_trivial_copy_constructor; 75 typedef __false_type has_trivial_assignment_operator; 76 typedef __false_type has_trivial_destructor; 77 typedef __false_type is_POD_type; 78 }; 79 80 template <> 81 struct __type_traits<int> 82 { 83 typedef __true_type has_trivial_default_constructor; 84 typedef __true_type has_trivial_copy_constructor; 85 typedef __true_type has_trivial_assignment_operator; 86 typedef __true_type has_trivial_destructor; 87 typedef __true_type is_POD_type; 88 }; 89 90 template <class ForwardIterator, class Size, class T> 91 inline ForwardIterator uninitialized_fill_n(ForwardIterator first, Size n, const T& x) 92 { 93 return __uninitialized_fill_n( first, n, x, value_type(first)); 94 } 95 96 template <class ForwardIterator, class Size, class T, class T1> 97 inline ForwardIterator __uninitialized_fill_n( ForwardIterator first, Size n, const T& x, T1* ) 98 { 99 typedef typename __type_traits<T1>::is_POD_type is_POD; 100 return __uninitialized_fill_n_aux( first, n, x, is_POD()); 101 } 102 103 template <class ForwardIterator, class Size, class T> 104 inline ForwardIterator __uninitialized_fill_n_aux( ForwardIterator first, Size n, const T& x, __true_type ) 105 { 106 return fill_n( first, n, x); 107 } 108 109 template <class ForwardIterator, class Size, class T> 110 ForwardIterator __uninitialized_fill_n_aux( ForwardIterator first, Size n, const T& x, __false_type) 111 { 112 ForwardIterator cur = first; 113 for( ; n > 0; --n, ++cur ) 114 { 115 construct( &*cur, x); 116 } 117 return cur; 118 } 119 120 class Test 121 { 122 public: 123 Test(int n = 0) 124 { 125 std::cout << "Test::Test(" << n << ")被呼叫\n"; 126 data = n; 127 } 128 int operator()() { return data; } 129 int operator int() { return data; } 130 private: 131 int data; 132 }; 133 134 int main() 135 { 136 int a[] = { 1, 2, 3, 4, 5 }; 137 int *begin = &a[0]; 138 uninitialized_fill_n(begin, 5, 1); 139 140 for( int i = 0; i < 5; i++ ) 141 std::cout << a[i] << ' '; //輸出5個1 142 143 std::cout << std::endl; 144 Test array[5]; //呼叫5次建構函式 145 Test *pT = &array[0]; 146 uninitialized_fill_n( pT, 5, 2); //呼叫5次建構函式 147 148 for( int i = 0; i < 5; i++ ) 149 std::cout << array[i] << ' '; //輸出5個2 150 151 std::cout << std::endl; 152 system("pause"); 153 return 0; 154 }
  1 /* https://blog.csdn.net/damotiansheng/article/details/41379151 */
  2 
  3 #include <iostream>
  4 #include <new.h>
  5 //using namespace std; //不要std,因為std名稱空間中也有input_iterator_tag等的定義
  6  
  7 struct input_iterator_tag {};   //std名稱空間中也有input_iterator_tag的定義
  8 struct output_iterator_tag {};
  9 struct forward_iterator_tag: public input_iterator_tag {};
 10 struct bidirectional_iterator_tag: public forward_iterator_tag {};
 11 struct random_access_iterator_tag: public bidirectional_iterator_tag {};
 12 
 13 struct __true_type {};
 14 struct __false_type {};
 15  
 16 template <class T1, class T2>
 17 inline void construct( T1 *p, const T2& value )
 18 {
 19     new (p) T1(value);
 20     return;
 21 }
 22  
 23  
 24 template <class OutputIterator, class Size, class T>
 25 OutputIterator fill_n( OutputIterator first, Size n, const T& value)
 26 {
 27     for( ; n > 0; --n, ++first)
 28     {
 29         *first = value;
 30     }
 31     return first;
 32 }
 33  
 34 template <class Category, class T, class Distance = ptrdiff_t, class Pointer = T*, class Reference = T&>
 35 struct iterator
 36 {
 37     typedef Category iterator_category;
 38     typedef T value_type;
 39     typedef Distance difference_type;
 40     typedef Pointer pointer;
 41     typedef Reference reference;
 42 };
 43  
 44 template <class Iterator>
 45 struct iterator_traits  // iterator_traits負責萃取迭代器的特性,例如獲得迭代器指向元素的型別等
 46 {
 47     typedef typename Iterator::iterator_category iterator_category;
 48     typedef typename Iterator::value_type value_type;
 49     typedef typename Iterator::difference_type difference_type;
 50     typedef typename Iterator::pointer pointer;
 51     typedef typename Iterator::reference reference;
 52 };
 53  
 54 template <class T>
 55 struct iterator_traits<T*>
 56 {
 57     typedef random_access_iterator_tag iterator_category;
 58     typedef T value_type;
 59     typedef ptrdiff_t difference_type;
 60     typedef T* pointer;
 61     typedef T& reference;
 62 };
 63  
 64 template <class Iterator>
 65 inline typename iterator_traits<Iterator>::value_type* value_type( const Iterator&)
 66 {
 67     return static_cast<typename iterator_traits<Iterator>::value_type*>(0);
 68 }
 69  
 70 template <class type>
 71 struct __type_traits
 72 {
 73     typedef __false_type has_trivial_default_constructor;
 74     typedef __false_type has_trivial_copy_constructor;
 75     typedef __false_type has_trivial_assignment_operator;
 76     typedef __false_type has_trivial_destructor;
 77     typedef __false_type is_POD_type;
 78 };
 79  
 80 template <>
 81 struct __type_traits<int>
 82 {
 83     typedef __true_type has_trivial_default_constructor;
 84     typedef __true_type has_trivial_copy_constructor;
 85     typedef __true_type has_trivial_assignment_operator;
 86     typedef __true_type has_trivial_destructor;
 87     typedef __true_type is_POD_type;
 88 };
 89  
 90 template <class ForwardIterator, class Size, class T>
 91 inline ForwardIterator uninitialized_fill_n(ForwardIterator first, Size n, const T& x)
 92 {
 93     return __uninitialized_fill_n( first, n, x, value_type(first));
 94 }
 95  
 96 template <class ForwardIterator, class Size, class T, class T1>
 97 inline ForwardIterator __uninitialized_fill_n( ForwardIterator first, Size n, const T& x, T1* )
 98 {
 99     typedef typename __type_traits<T1>::is_POD_type is_POD;
100     return __uninitialized_fill_n_aux( first, n, x, is_POD()); 
101 }
102  
103 template <class ForwardIterator, class Size, class T>
104 inline ForwardIterator __uninitialized_fill_n_aux( ForwardIterator first, Size n, const T& x, __true_type )
105 {
106     return fill_n( first, n, x);
107 }
108  
109 template <class ForwardIterator, class Size, class T>
110 ForwardIterator __uninitialized_fill_n_aux( ForwardIterator first, Size n, const T& x, __false_type)
111 {
112     ForwardIterator cur = first;
113     for( ; n > 0; --n, ++cur )
114     {
115         construct( &*cur, x);
116     }
117     return cur;
118 }
119  
120 class Test
121 {
122 public:
123     Test(int n = 0)
124     {
125         std::cout << "Test::Test(" << n << ")被呼叫\n";
126         data = n;
127     }
128     int operator()() { return data; }
129     int operator int() { return data; }
130 private:
131     int data;
132 };
133  
134 int main()
135 {
136     int a[] = { 1, 2, 3, 4, 5 };
137     int *begin = &a[0];
138     uninitialized_fill_n(begin, 5, 1);
139     
140     for( int i = 0; i < 5; i++ )
141         std::cout << a[i] << ' ';  //輸出5個1
142  
143     std::cout << std::endl;
144     Test array[5];  //呼叫5次建構函式
145     Test *pT = &array[0]; 
146     uninitialized_fill_n( pT, 5, 2); //呼叫5次建構函式
147 
148     for( int i = 0; i < 5; i++ )
149         std::cout << array[i] << ' '; //輸出5個2
150 
151     std::cout << std::endl;
152     system("pause");
153     return 0;
154 }
View Code