From c5f79243992c79111ad1a6fee11628f511c1cdc6 Mon Sep 17 00:00:00 2001 From: Denes Matetelki Date: Tue, 30 Apr 2013 20:52:14 +0200 Subject: [PATCH] iterators can be used in a const situation too --- graph.hpp | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/graph.hpp b/graph.hpp index 8728146..3a7380a 100644 --- a/graph.hpp +++ b/graph.hpp @@ -95,8 +95,8 @@ public: reference_self_type operator=(self_type o) { swap(o); return *this; } void swap(reference_self_type o) { std::swap(m_it, o.m_it); } - reference operator*() { return *((*m_it).m_data); } - pointer operator->() { return (*m_it).m_data; } + const_reference operator*() { return *((*m_it).m_data); } + const_pointer operator->() { return (*m_it).m_data; } self_type &operator++() { ++m_it; return *this; } self_type operator++(int) { self_type tmp(*this); ++(*this); return tmp; } self_type operator+(difference_type n) { self_type tmp(*this); tmp.pos_ += n; return tmp; } @@ -106,12 +106,15 @@ public: private: vertex_iterator(typename std::vector::iterator it) : m_it(it) {} + vertex_iterator(typename std::vector::const_iterator it) : m_it(it) {} - typename std::vector::iterator m_it; + typename std::vector::const_iterator m_it; }; vertex_iterator vertex_begin() { return vertex_iterator(m_vertices.begin()); } + const vertex_iterator vertex_begin() const { return vertex_iterator(m_vertices.begin()); } vertex_iterator vertex_end() { return vertex_iterator(m_vertices.end()); } + const vertex_iterator vertex_end() const { return vertex_iterator(m_vertices.end()); } class edge_iterator : public std::iterator() { resetEdge(); return &m_edge; } + const edge_reference operator*() { resetEdge(); return m_edge; } + const edge_pointer operator->() { resetEdge(); return &m_edge; } self_type &operator++() { advance(1); return *this; } self_type operator++(int) { self_type tmp(*this); advance(1); return tmp; } self_type operator+(difference_type n) { self_type tmp(*this); tmp.pos_ += n; return tmp; } @@ -154,7 +157,9 @@ public: }; edge_iterator edge_begin() { return edge_iterator(m_vertices); } + const edge_iterator edge_begin() const { return edge_iterator(m_vertices); } edge_iterator edge_end() { return edge_iterator(m_vertices, false); } + const edge_iterator edge_end() const { return edge_iterator(m_vertices, false); } private: