Newsgroup: it.comp.lang.c++
Titolo: #include <iterator> ... quando è nec essaria?

Quando si fanno uso di iteratori bisogna includere sempre anche la libreria 
<iterator> ??
Lo chiedo perchè finora, pur usando iteratori, non l'avevo mai inclusa, ma 
dovendo usare un ostream_iterator, l'ho dovuta includere. E' buona norma 
includerla quando si usano iteratori in generale, o la si include solo 
quando viene espressamente richiesta (come in questo caso).
Il tutto era uscito fuori da questo esercizio che stavo sperimentando:
 #include <algorithm>
 #include <vector>
 #include <iostream>
 #include <iterator>
 using namespace std;
 int main()
 {
   int a[6] = {1,2,3,4,5,6};
   vector<int> v1(a, a+6);
   ostream_iterator<int,char> out(cout," ");
   copy(v1.begin(),v1.end(),out);
   cout << endl;
   system("PAUSE");	
   return 0;
}
Ciao, e grazie per i vostri consigli.
Checco

    vector<int> v1(a, a+6);
[cut]
consiglio un po OT ... non usare a+6 come fine del vettore, rendilo auto
definito in fase di compilazione facendo:
vector<int> v1(a,a+(sizeof(a)/sizeof(a[0])));
cosi facendo puoi amliare il vettore o ridurlo quando vuoi senza doverti
ricordare di andare a cambiare tutti i suoi riferimenti nel codice ...
ciao

Francesco wrote:

questo e' il file iterator.h delle libstdc++:
-----------
iterator.h
[CUT]
00027 #ifndef _CPP_BACKWARD_ITERATOR_H
00028 #define _CPP_BACKWARD_ITERATOR_H 1
00029 
00030 #include "backward_warning.h"
00031 #include "function.h"
00032 #include <stddef.h>
00033 #include "iostream.h"
00034 #include <iterator>
00035 
00036 #include <bits/stl_construct.h>
00037 #include <bits/stl_raw_storage_iter.h>
00038 
00039 #include <ext/iterator> // For 3-parameter distance extension
00040 
00041 // Names from stl_iterator.h
00042 using std::input_iterator_tag;
00043 using std::output_iterator_tag;
00044 using std::forward_iterator_tag;
00045 using std::bidirectional_iterator_tag;
00046 using std::random_access_iterator_tag;
00047 
00048 #if 0
00049 using std::iterator;
00050 #endif
00051 
00052 // The base classes input_iterator, output_iterator, forward_iterator,
00053 // bidirectional_iterator, and random_access_iterator are not part of
00054 // the C++ standard.  (They have been replaced by struct iterator.)
00055 // They are included for backward compatibility with the HP STL.
00056 template<typename _Tp, typename _Distance>
00057   struct input_iterator {
00058     typedef input_iterator_tag iterator_category;
00059     typedef _Tp                value_type;
00060     typedef _Distance          difference_type;
00061     typedef _Tp*               pointer;
00062     typedef _Tp&               reference;
00063   };
00064 
00065 struct output_iterator {
00066   typedef output_iterator_tag iterator_category;
00067   typedef void                value_type;
00068   typedef void                difference_type;
00069   typedef void                pointer;
00070   typedef void                reference;
00071 };
00072 
00073 template<typename _Tp, typename _Distance>
00074   struct forward_iterator {
00075     typedef forward_iterator_tag iterator_category;
00076     typedef _Tp                  value_type;
00077     typedef _Distance            difference_type;
00078     typedef _Tp*                 pointer;
00079     typedef _Tp&                 reference;
00080   };
00081 
00082 template<typename _Tp, typename _Distance>
00083   struct bidirectional_iterator {
00084     typedef bidirectional_iterator_tag iterator_category;
00085     typedef _Tp                        value_type;
00086     typedef _Distance                  difference_type;
00087     typedef _Tp*                       pointer;
00088     typedef _Tp&                       reference;
00089   };
00090 
00091 template<typename _Tp, typename _Distance>
00092   struct random_access_iterator {
00093     typedef random_access_iterator_tag iterator_category;
00094     typedef _Tp                        value_type;
00095     typedef _Distance                  difference_type;
00096     typedef _Tp*                       pointer;
00097     typedef _Tp&                       reference;
00098   };
00099 
00100 using std::iterator_traits;
00101 
00102 template <class _Iter>
00103   inline typename iterator_traits<_Iter>::iterator_category
00104   iterator_category(const _Iter& __i)
00105   { return __iterator_category(__i); }
00106 
00107 template <class _Iter>
00108   inline typename iterator_traits<_Iter>::difference_type*
00109   distance_type(const _Iter&)
00110   { return static_cast<typename
iterator_traits<_Iter>::difference_type*>(0); }
00111 
00112 template<class _Iter>
00113   inline typename iterator_traits<_Iter>::value_type*
00114   value_type(const _Iter& __i)
00115   { return static_cast<typename
iterator_traits<_Iter>::value_type*>(0); }
00116 
00117 using std::distance;
00118 using __gnu_cxx::distance; // 3-parameter extension
00119 using std::advance; 
00120 
00121 using std::insert_iterator;
00122 using std::front_insert_iterator;
00123 using std::back_insert_iterator;
00124 using std::inserter;
00125 using std::front_inserter;
00126 using std::back_inserter;
00127 
00128 using std::reverse_iterator;
00129 
00130 using std::istream_iterator;
00131 using std::ostream_iterator;
00132 
00133 // Names from stl_construct.h
00134 template<class _T1, class _T2>
00135   inline void
00136   construct(_T1* __p, const _T2& __value)
00137   { std::_Construct(__p, __value); }
00138 
00139 template<class _T1>
00140   inline void
00141   construct(_T1* __p)
00142   { std::_Construct(__p); }
00143 
00144 template <class _Tp>
00145   inline void
00146   destroy(_Tp* __pointer)
00147   { std::_Destroy(__pointer); }
00148   
00149 template <class _ForwardIterator>
00150   inline void
00151   destroy(_ForwardIterator __first, _ForwardIterator __last)
00152   { std::_Destroy(__first, __last); }
00153 
00154 
00155 // Names from stl_raw_storage_iter.h
00156 using std::raw_storage_iterator;
00157 
00158 #endif /* _CPP_BACKWARD_ITERATOR_H */
00159 
00160 // Local Variables:
00161 // mode:C++
00162 // End:
----------------------------------