File: iterator

package info (click to toggle)
cppreference-doc 20170409-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 259,872 kB
  • sloc: xml: 570,184; python: 1,923; php: 520; makefile: 168; sh: 25; cpp: 9; ansic: 9
file content (524 lines) | stat: -rw-r--r-- 16,943 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
/*  Copyright (C) 2015  Povilas Kanapickas <povilas@radix.lt>

    This file is part of cppreference-doc

    This work is licensed under the Creative Commons Attribution-ShareAlike 3.0
    Unported License. To view a copy of this license, visit
    http://creativecommons.org/licenses/by-sa/3.0/ or send a letter to Creative
    Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.

    Permission is granted to copy, distribute and/or modify this document
    under the terms of the GNU Free Documentation License, Version 1.3 or
    any later version published by the Free Software Foundation; with no
    Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
*/

#ifndef CPPREFERENCE_ITERATOR_H
#define CPPREFERENCE_ITERATOR_H

namespace std {

// SIMPLIFIED: template specializations are not supported, so the typedefs use
// the T* specialization
template<class Iterator>
struct iterator_traits {
#if CPPREFERENCE_SIMPLIFY_TYPEDEFS
    typedef ptrdiff_t difference_type;
    typedef T value_type;
    typedef T* pointer;
    typedef T& reference;
    typedef random_access_iterator_tag iterator_category;
#else
    typedef typename Iterator::difference_type difference_type;
    typedef typename Iterator::value_type value_type;
    typedef typename Iterator::pointer pointer;
    typedef typename Iterator::reference reference;
    typedef typename Iterator::iterator_category iterator_category;
#endif
};

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 Category, class T, class Distance = ptrdiff_t,
         class Pointer = T*, class Reference = T&>
struct iterator {
    typedef T value_type;
    typedef Distance difference_type;
    typedef Pointer pointer;
    typedef Reference reference;
    typedef Category iterator_category;
};

template <class Iterator>
class reverse_iterator { // SIMPLIFIED: does not inherit iterator
public:
    typedef Iterator iterator_type;
#if CPPREFERENCE_SIMPLIFY_TYPEDEFS
    typedef typename ptrdiff_t difference_type;
    typedef typename Iterator::value_type value_type;
    typedef typename Iterator::pointer pointer;
    typedef typename Iterator::reference reference;
    typedef typename Iterator::iterator_category iterator_category;
#else
    typedef typename iterator_traits<Iterator>::value_type value_type;
    typedef typename iterator_traits<Iterator>::iterator_category iterator_category;
    typedef typename iterator_traits<Iterator>::difference_type difference_type;
    typedef typename iterator_traits<Iterator>::reference reference;
    typedef typename iterator_traits<Iterator>::pointer pointer;
#endif

    reverse_iterator();
    explicit reverse_iterator(Iterator x);
    template <class U> reverse_iterator(const reverse_iterator<U>& other);

    template<class U>
    reverse_iterator& operator=(const reverse_iterator<U>& other);

    Iterator base() const;
    reference operator*() const;
    pointer operator->() const;
    reference operator[](difference_type n) const; // actually unspecified

    reverse_iterator& operator++();
    reverse_iterator& operator--();
    reverse_iterator operator++(int);
    reverse_iterator operator--(int);
    reverse_iterator operator+(difference_type n) const;
    reverse_iterator operator-(difference_type n) const;
    reverse_iterator& operator+=(difference_type n);
    reverse_iterator& operator-=(difference_type n);
protected:
    Iterator current;
};

template<class Iterator1, class Iterator2 >
bool operator==(const reverse_iterator<Iterator1>& lhs,
                const reverse_iterator<Iterator2>& rhs);

template<class Iterator1, class Iterator2 >
bool operator!=(const reverse_iterator<Iterator1>& lhs,
                const reverse_iterator<Iterator2>& rhs);

template<class Iterator1, class Iterator2 >
bool operator<(const reverse_iterator<Iterator1>& lhs,
               const reverse_iterator<Iterator2>& rhs);

template<class Iterator1, class Iterator2 >
bool operator<=(const reverse_iterator<Iterator1>& lhs,
                const reverse_iterator<Iterator2>& rhs);

template<class Iterator1, class Iterator2 >
bool operator>(const reverse_iterator<Iterator1>& lhs,
               const reverse_iterator<Iterator2>& rhs);

template<class Iterator1, class Iterator2 >
bool operator>=(const reverse_iterator<Iterator1>& lhs,
                const reverse_iterator<Iterator2>& rhs);

template<class Iterator >
reverse_iterator<Iterator>
operator+(typename reverse_iterator<Iterator>::difference_type n,
          const reverse_iterator<Iterator>& it);

template<class Iterator >
typename reverse_iterator<Iterator>::difference_type
operator-(const reverse_iterator<Iterator>& lhs,
          const reverse_iterator<Iterator>& rhs);

#if CPPREFERENCE_STDVER >= 2011
template <class Iterator>
class move_iterator {
public:
    typedef Iterator iterator_type;
    typedef Iterator pointer;
    typedef value_type&& reference;
#if CPPREFERENCE_SIMPLIFY_TYPEDEFS
    typedef typename ptrdiff_t difference_type;
    typedef typename Iterator::iterator_category iterator_category;
#else
    typedef typename iterator_traits<Iterator>::difference_type difference_type;
    typedef typename iterator_traits<Iterator>::iterator_category iterator_category;
#endif

    move_iterator();
    explicit move_iterator(Iterator x);
    template <class U> move_iterator(const move_iterator<U>& other);

    template<class U>
    move_iterator& operator=(const move_iterator<U>& other);

    Iterator base() const;
    reference operator*() const;
    pointer operator->() const;
    reference operator[](difference_type n) const; // actually unspecified

    move_iterator& operator++();
    move_iterator& operator--();
    move_iterator operator++(int);
    move_iterator operator--(int);
    move_iterator operator+(difference_type n) const;
    move_iterator operator-(difference_type n) const;
    move_iterator& operator+=(difference_type n);
    move_iterator& operator-=(difference_type n);
};


template<class Iterator1, class Iterator2 >
bool operator==(const move_iterator<Iterator1>& lhs,
                const move_iterator<Iterator2>& rhs);

template<class Iterator1, class Iterator2 >
bool operator!=(const move_iterator<Iterator1>& lhs,
                const move_iterator<Iterator2>& rhs);

template<class Iterator1, class Iterator2 >
bool operator<(const move_iterator<Iterator1>& lhs,
               const move_iterator<Iterator2>& rhs);

template<class Iterator1, class Iterator2 >
bool operator<=(const move_iterator<Iterator1>& lhs,
                const move_iterator<Iterator2>& rhs);

template<class Iterator1, class Iterator2 >
bool operator>(const move_iterator<Iterator1>& lhs,
               const move_iterator<Iterator2>& rhs);

template<class Iterator1, class Iterator2 >
bool operator>=(const move_iterator<Iterator1>& lhs,
                const move_iterator<Iterator2>& rhs);

template<class Iterator >
move_iterator<Iterator>
operator+(typename move_iterator<Iterator>::difference_type n,
          const move_iterator<Iterator>& it);

template<class Iterator >
typename move_iterator<Iterator>::difference_type
operator-(const move_iterator<Iterator>& lhs,
          const move_iterator<Iterator>& rhs);

#endif // CPPREFERENCE_STDVER >= 2011

template<class Container>
class back_insert_iterator { // SIMPLIFIED: does not inherit iterator
public:
    typedef void value_type;
    typedef void difference_type;
    typedef void pointer;
    typedef void reference;
    typedef output_iterator_tag iterator_category;

    typedef Container container_type;

    explicit back_insert_iterator(Container& c);

#if CPPREFERENCE_STDVER <2011
    back_insert_iterator<Container>&
    operator=(typename Container::const_reference value);
#else
    back_insert_iterator<Container>&
    operator=(const typename Container::value_type& value);
#endif
    back_insert_iterator<Container>&
    operator=(typename Container::value_type&& value);
    back_insert_iterator& operator*();
    back_insert_iterator& operator++();
    back_insert_iterator& operator++(int);
protected:
    Container* container;
};

template<class Container>
class front_insert_iterator { // SIMPLIFIED: does not inherit iterator
public:
    typedef void value_type;
    typedef void difference_type;
    typedef void pointer;
    typedef void reference;
    typedef output_iterator_tag iterator_category;

    typedef Container container_type;

    explicit front_insert_iterator(Container& c);

#if CPPREFERENCE_STDVER <2011
    front_insert_iterator<Container>&
    operator=(typename Container::const_reference value);
#else
    front_insert_iterator<Container>&
    operator=(const typename Container::value_type& value);
#endif
    front_insert_iterator<Container>&
    operator=(typename Container::value_type&& value);
    front_insert_iterator& operator*();
    front_insert_iterator& operator++();
    front_insert_iterator& operator++(int);
protected:
    Container* container;
};

template<class Container>
class insert_iterator { // SIMPLIFIED: does not inherit iterator
public:
    typedef void value_type;
    typedef void difference_type;
    typedef void pointer;
    typedef void reference;
    typedef output_iterator_tag iterator_category;

    typedef Container container_type;

    explicit insert_iterator(Container& c, typename Container::iterator i);

#if CPPREFERENCE_STDVER <2011
    insert_iterator<Container>&
    operator=(typename Container::const_reference value);
#else
    insert_iterator<Container>&
    operator=(const typename Container::value_type& value);
#endif
    insert_iterator<Container>&
    operator=(typename Container::value_type&& value);
    insert_iterator& operator*();
    insert_iterator& operator++();
    insert_iterator& operator++(int);
protected:
    Container* container;
    Container::iterator iter;
};

template<class T,
         class CharT = char,
         class Traits = std::char_traits<CharT>,
         class Distance = std::ptrdiff_t >
class istream_iterator { // SIMPLIFIED: does not inherit iterator
public:
    typedef CharT char_type;
    typedef Traits traits_type;
    typedef std::basic_istream<CharT, Traits> istream_type;

    typedef T value_type;
    typedef Distance difference_type;
    typedef const T* pointer;
    typedef const T& reference;
    typedef input_iterator_tag iterator_category;

    istream_iterator();

    istream_iterator(istream_type& stream);

    istream_iterator(const istream_iterator& other) = default;

    const T& operator*() const;
    const T* operator->() const;

    istream_iterator& operator++();
    istream_iterator operator++(int);
};

template<class T, class CharT, class Traits, class Dist >
bool operator==(const istream_iterator<T, CharT, Traits, Dist>& lhs,
                const istream_iterator<T, CharT, Traits, Dist>& rhs);

template<class CharT, class Traits >
bool operator!=(const istream_iterator<T, CharT, Traits, Dist>& lhs,
                const istream_iterator<T, CharT, Traits, Dist>& rhs);


template<class T,
         class CharT = char,
         class Traits = std::char_traits<CharT> >
class ostream_iterator { // SIMPLIFIED: does not inherit iterator
public:
    typedef CharT char_type;
    typedef Traits traits_type;
    typedef std::basic_ostream<CharT, Traits> ostream_type;

    typedef void value_type;
    typedef void difference_type;
    typedef void pointer;
    typedef void reference;
    typedef output_iterator_tag iterator_category;

    ostream_iterator(ostream_type& stream);
    ostream_iterator(ostream_type& stream, const CharT* delim);
    ~ostream_iterator();

    ostream_iterator& operator=(const T& value);
    ostream_iterator& operator*();
    ostream_iterator& operator++();
    ostream_iterator& operator++(int);
};

template<class T, class CharT, class Traits >
bool operator==(const ostream_iterator<T, CharT, Traits>& lhs,
                const ostream_iterator<T, CharT, Traits>& rhs);

template<class CharT, class Traits >
bool operator!=(const ostream_iterator<T, CharT, Traits>& lhs,
                const ostream_iterator<T, CharT, Traits>& rhs);


template<class CharT, class Traits = std::char_traits<CharT> >
class istreambuf_iterator { // SIMPLIFIED: does not inherit iterator
public:
    typedef CharT char_type;
    typedef Traits traits_type;
#if CPPREFERENCE_SIMPLIFY_TYPEDEFS
    typedef int int_type;
#else
    typedef typename Traits::int_type int_type;
#endif
    typedef std::basic_streambuf<CharT, Traits> streambuf_type;
    typedef std::basic_istream<CharT, Traits> istream_type;

    typedef T value_type;
    typedef Distance difference_type;
    typedef const T* pointer;
    typedef const T& reference;
    typedef input_iterator_tag iterator_category;

    istreambuf_iterator();
    istreambuf_iterator(std::basic_istream<CharT, Traits>& is);
    istreambuf_iterator(std::basic_streambuf<CharT, Traits>* s);
    istreambuf_iterator(const istreambuf_iterator&) = default;

    CharT operator*() const;
#if CPPREFERENCE_STDVER >= 2011
    pointer operator->() const;
#endif
    istreambuf_iterator& operator++();
    istreambuf_iterator operator++(int); // actually unspecified

    bool equal(const istreambuf_iterator& it) const;
};

template<class CharT, class Traits >
bool operator==(const istreambuf_iterator<CharT, Traits>& lhs,
                const istreambuf_iterator<CharT, Traits>& rhs);

template<class CharT, class Traits >
bool operator!=(const istreambuf_iterator<CharT, Traits>& lhs,
                const istreambuf_iterator<CharT, Traits>& rhs);

template<class CharT,
         class Traits = std::char_traits<CharT> >
class ostreambuf_iterator { // SIMPLIFIED: does not inherit iterator
public:
    typedef CharT char_type;
    typedef Traits traits_type;
    typedef std::basic_streambuf<CharT, Traits> streambuf_type;
    typedef std::basic_ostream<CharT, Traits> ostream_type;

    typedef void value_type;
    typedef void difference_type;
    typedef void pointer;
    typedef void reference;
    typedef output_iterator_tag iterator_category;

    ostreambuf_iterator(streambuf_type* buffer);
    ostreambuf_iterator(ostream_type& stream);

    ostreambuf_iterator& operator=(CharT c);
    ostreambuf_iterator& operator*();
    ostreambuf_iterator& operator++();
    ostreambuf_iterator& operator++(int);
    bool failed() const;
};

#if CPPREFERENCE_STDVER >= 2011 && CPPREFERENCE_STDVER <2014
template<class Iterator >
std::move_iterator<Iterator> make_move_iterator(const Iterator& i);
#elif CPPREFERENCE_STDVER >= 2014
template<class Iterator >
std::move_iterator<Iterator> make_move_iterator(Iterator i);

template <class Iterator>
std::reverse_iterator<Iterator> make_reverse_iterator(Iterator i);
#endif

template<class Container >
std::front_insert_iterator<Container> front_inserter(Container& c);

template<class Container >
std::back_insert_iterator<Container> back_inserter(Container& c);

template<class Container >
std::insert_iterator<Container> inserter(Container& c, typename Container::iterator i);

template<class InputIt, class Distance >
void advance(InputIt& it, Distance n);

template<class InputIt >
typename std::iterator_traits<InputIt>::difference_type
distance(InputIt first, InputIt last);

#if CPPREFERENCE_STDVER >= 2011
template<class ForwardIt >
ForwardIt next(ForwardIt it,
               typename std::iterator_traits<ForwardIt>::difference_type n = 1);

template<class BidirIt >
BidirIt prev(BidirIt it,
             typename std::iterator_traits<BidirIt>::difference_type n = 1);

template<class C >
auto begin(C& c) -> decltype(c.begin());

template<class C >
auto begin(const C& c) -> decltype(c.begin());

template<class T, std::size_t N >
T* begin(T(&array)[N]);

template<class C >
auto end(C& c) -> decltype(c.end());

template<class C >
auto end(const C& c) -> decltype(c.end());

template<class T, std::size_t N >
T* end(T(&array)[N]);

#endif
#if CPPREFERENCE_STDVER >= 2014

template<class C >
constexpr auto cbegin(const C& c) -> decltype(std::begin(c));

template<class C >
constexpr auto cend(const C& c) -> decltype(std::end(c));

template<class C >
auto rbegin(C& c) -> decltype(c.rbegin());

template<class C >
auto rbegin(const C& c) -> decltype(c.rbegin());

template<class T, size_t N >
reverse_iterator<T*> rbegin(T(&array)[N]);

template<class C >
auto crbegin(const C& c) -> decltype(std::rbegin(c));

template<class C >
auto rend(C& c) -> decltype(c.rend());

template<class C >
auto rend(const C& c) -> decltype(c.rend());

template<class T, size_t N >
reverse_iterator<T*> rend(T(&array)[N]);

template<class C >
auto crend(const C& c) -> decltype(std::rend(c));

#endif // CPPREFERENCE_STDVER >= 2014

} // namespace std

#endif // CPPREFERENCE_ITERATOR_H