1 // Demonstrate and test boost/operators.hpp on std::iterators --------------//
3 // (C) Copyright Jeremy Siek 1999. Permission to copy, use, modify,
4 // sell and distribute this software is granted provided this
5 // copyright notice appears in all copies. This software is provided
6 // "as is" without express or implied warranty, and with no claim as
7 // to its suitability for any purpose.
9 // See http://www.boost.org for most recent version including documentation.
12 // 12 Dec 99 Initial version with iterator operators (Jeremy Siek)
18 #include <boost/operators.hpp>
19 using namespace boost
;
22 template <class T
, class R
, class P
>
24 : public boost::random_access_iterator_helper
<
25 test_iter
<T
,R
,P
>, T
, std::ptrdiff_t, P
, R
>
27 typedef test_iter self
;
29 typedef std::ptrdiff_t Distance
;
32 test_iter(T
* i
) : _i(i
) { }
33 test_iter(const self
& x
) : _i(x
._i
) { }
34 self
& operator=(const self
& x
) { _i
= x
._i
; return *this; }
35 Reference
operator*() const { return *_i
; }
36 self
& operator++() { ++_i
; return *this; }
37 self
& operator--() { --_i
; return *this; }
38 self
& operator+=(Distance n
) { _i
+= n
; return *this; }
39 self
& operator-=(Distance n
) { _i
-= n
; return *this; }
40 bool operator==(const self
& x
) const { return _i
== x
._i
; }
41 bool operator<(const self
& x
) const { return _i
< x
._i
; }
42 friend Distance
operator-(const self
& x
, const self
& y
) {
53 string array
[] = { "apple", "orange", "pear", "peach", "grape", "plum" };
55 test_iter
<string
,string
&,string
*> i
= array
,
56 ie
= array
+ sizeof(array
)/sizeof(string
);
58 // Tests for all of the operators added by random_access_iterator_helper
72 ie
= array
+ sizeof(array
)/sizeof(string
);
76 cout
<< i
->size() << " ";
104 ie
= array
+ sizeof(array
)/sizeof(string
);
107 for (std::size_t j
= 0; j
< sizeof(array
)/sizeof(string
); ++j
)
112 test_iter
<string
, const string
&, const string
*> i
= array
,
113 ie
= array
+ sizeof(array
)/sizeof(string
);
115 // Tests for all of the operators added by random_access_iterator_helper
129 ie
= array
+ sizeof(array
)/sizeof(string
);
133 cout
<< i
->size() << " ";
150 i
= ptrdiff_t(2) + i
;
161 ie
= array
+ sizeof(array
)/sizeof(string
);
164 for (std::size_t j
= 0; j
< sizeof(array
)/sizeof(string
); ++j
)