comparison algs4-c++/src/Queue.hpp @ 26:2cbfacd2a3e9

Slight tweak to the iterator for Queue to make it return values by reference.
author Eris Caffee <discordia@eldalin.com>
date Tue, 23 Jun 2015 15:46:22 -0500
parents 3cdac4c29445
children 80ca1973e3bd
comparison
equal deleted inserted replaced
5:89f5673d1606 6:13914132f575
27 template <bool is_const = true> 27 template <bool is_const = true>
28 class generic_iterator : public std::iterator<std::forward_iterator_tag, T> { 28 class generic_iterator : public std::iterator<std::forward_iterator_tag, T> {
29 29
30 private: 30 private:
31 typedef typename std::conditional<is_const, const Node *, Node *>::type NodePtrType; 31 typedef typename std::conditional<is_const, const Node *, Node *>::type NodePtrType;
32 typedef typename std::conditional<is_const, const T, T>::type ValueType; 32 typedef typename std::conditional<is_const, const T&, T&>::type ValueReferenceType;
33 33
34 public: 34 public:
35 35
36 generic_iterator( NodePtrType c ) : 36 generic_iterator( NodePtrType c ) :
37 curr (c) 37 curr (c)
52 this->curr = this->curr->next; 52 this->curr = this->curr->next;
53 } 53 }
54 return t; 54 return t;
55 } 55 }
56 56
57 ValueType operator*() { 57 ValueReferenceType operator*() {
58 return this->curr->item; 58 return this->curr->item;
59 } 59 }
60 60
61 bool operator!=( generic_iterator other ) { 61 bool operator!=( generic_iterator other ) {
62 return this->curr != other.curr; 62 return this->curr != other.curr;