diff --git a/graph.h b/graph.h index 7c64d57..096c3e2 100644 --- a/graph.h +++ b/graph.h @@ -91,7 +91,7 @@ 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); } - pointer operator*() { return (*m_it).m_data; } + reference operator*() { return *((*m_it).m_data); } 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; } @@ -122,13 +122,13 @@ public: typedef edge_iterator& reference_self_type; typedef const edge_iterator& const_reference_self_type; - edge_iterator() : m_vertices(), m_vertex_it(), m_edge_it(), m_edge(0) {} - ~edge_iterator() { if (m_edge) delete m_edge; } + edge_iterator() : m_vertices(), m_vertex_it(), m_edge_it(), m_edge() {} + ~edge_iterator() {} edge_iterator(const_reference_self_type o); reference_self_type operator=(self_type o) { swap(o); return *this; } void swap(reference_self_type other); - edge_reference operator*() { resetEdge(); return *m_edge; } + edge_reference operator*() { resetEdge(); return m_edge; } 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; } @@ -146,7 +146,7 @@ public: std::vector m_vertices; typename std::vector::iterator m_vertex_it; typename std::list::iterator m_edge_it; - edge_pointer m_edge; + Edge m_edge; }; edge_iterator edge_begin() { return edge_iterator(m_vertices); } @@ -228,7 +228,7 @@ Graph::edge_iterator::edge_iterator(const_reference_self_type o) : m_vertices(o.m_vertices) , m_vertex_it(o.m_vertex_it) , m_edge_it(o.m_edge_it) - , m_edge(0) + , m_edge() {} template @@ -254,7 +254,7 @@ void Graph::edge_iterator::swap(reference_self_type other) template Graph::edge_iterator::edge_iterator(std::vector vertices, bool begin) - : m_vertices(vertices), m_vertex_it(), m_edge_it(), m_edge(0) + : m_vertices(vertices), m_vertex_it(), m_edge_it(), m_edge() { if (begin) { m_vertex_it = m_vertices.begin(); @@ -271,14 +271,10 @@ Graph::edge_iterator::edge_iterator(std::vector vertices, bool begin) template void Graph::edge_iterator::resetEdge() { - if (m_edge) delete m_edge; - if (m_vertex_it == m_vertices.end() || (*m_vertex_it).m_edges.empty()) { - m_edge = 0; + m_edge = Edge(); } else { - m_edge = new Edge((*m_vertex_it).m_data, - (*m_edge_it).m_destination, - (*m_edge_it).m_weight); + m_edge = Edge((*m_vertex_it).m_data, (*m_edge_it).m_destination, (*m_edge_it).m_weight); } } diff --git a/main.cpp b/main.cpp index 3971291..65e8eb0 100644 --- a/main.cpp +++ b/main.cpp @@ -73,9 +73,9 @@ int main() ++g_it, ++v_it) { std::cout << "victor: " // << *g_it << " " - << **g_it + << *g_it << std::endl; - assert(**g_it == *v_it); + assert(*g_it == *v_it); } @@ -94,10 +94,9 @@ int main() g_it != g2.vertex_end(); ++g_it, ++v_it) { std::cout << "vector: " -// << *g_it << " " - << **g_it + << *g_it << std::endl; - assert(**g_it == *v_it); + assert(*g_it == *v_it); } @@ -135,9 +134,7 @@ int main() Graph::pointer source = (*edge_it).getSource(); Graph::pointer destination = (*edge_it).getDestination(); std::cout << "edge: " -// << source << " " << *source << " " -// << destination << " " << *destination << " " << (*edge_it).getWeight() << std::endl; }