dereferencing iterators with operator* return references instead of pointers

for/release
Denes Matetelki 12 years ago
parent 183081c17f
commit d02e826d41

@ -91,7 +91,7 @@ public:
reference_self_type operator=(self_type o) { swap(o); return *this; } reference_self_type operator=(self_type o) { swap(o); return *this; }
void swap(reference_self_type o) { std::swap(m_it, o.m_it); } 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; } pointer operator->() { return (*m_it).m_data; }
self_type &operator++() { ++m_it; return *this; } self_type &operator++() { ++m_it; return *this; }
self_type operator++(int) { self_type tmp(*this); ++(*this); return tmp; } self_type operator++(int) { self_type tmp(*this); ++(*this); return tmp; }
@ -122,13 +122,13 @@ public:
typedef edge_iterator& reference_self_type; typedef edge_iterator& reference_self_type;
typedef const edge_iterator& const_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() : m_vertices(), m_vertex_it(), m_edge_it(), m_edge() {}
~edge_iterator() { if (m_edge) delete m_edge; } ~edge_iterator() {}
edge_iterator(const_reference_self_type o); edge_iterator(const_reference_self_type o);
reference_self_type operator=(self_type o) { swap(o); return *this; } reference_self_type operator=(self_type o) { swap(o); return *this; }
void swap(reference_self_type other); 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; } edge_pointer operator->() { resetEdge(); return m_edge; }
self_type &operator++() { advance(1); return *this; } self_type &operator++() { advance(1); return *this; }
self_type operator++(int) { self_type tmp(*this); advance(1); return tmp; } self_type operator++(int) { self_type tmp(*this); advance(1); return tmp; }
@ -146,7 +146,7 @@ public:
std::vector<Vertex> m_vertices; std::vector<Vertex> m_vertices;
typename std::vector<Vertex>::iterator m_vertex_it; typename std::vector<Vertex>::iterator m_vertex_it;
typename std::list<EdgeTo>::iterator m_edge_it; typename std::list<EdgeTo>::iterator m_edge_it;
edge_pointer m_edge; Edge m_edge;
}; };
edge_iterator edge_begin() { return edge_iterator(m_vertices); } edge_iterator edge_begin() { return edge_iterator(m_vertices); }
@ -228,7 +228,7 @@ Graph<T>::edge_iterator::edge_iterator(const_reference_self_type o)
: m_vertices(o.m_vertices) : m_vertices(o.m_vertices)
, m_vertex_it(o.m_vertex_it) , m_vertex_it(o.m_vertex_it)
, m_edge_it(o.m_edge_it) , m_edge_it(o.m_edge_it)
, m_edge(0) , m_edge()
{} {}
template <typename T> template <typename T>
@ -254,7 +254,7 @@ void Graph<T>::edge_iterator::swap(reference_self_type other)
template <typename T> template <typename T>
Graph<T>::edge_iterator::edge_iterator(std::vector<Vertex> vertices, bool begin) Graph<T>::edge_iterator::edge_iterator(std::vector<Vertex> 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) { if (begin) {
m_vertex_it = m_vertices.begin(); m_vertex_it = m_vertices.begin();
@ -271,14 +271,10 @@ Graph<T>::edge_iterator::edge_iterator(std::vector<Vertex> vertices, bool begin)
template <typename T> template <typename T>
void Graph<T>::edge_iterator::resetEdge() void Graph<T>::edge_iterator::resetEdge()
{ {
if (m_edge) delete m_edge;
if (m_vertex_it == m_vertices.end() || (*m_vertex_it).m_edges.empty()) { if (m_vertex_it == m_vertices.end() || (*m_vertex_it).m_edges.empty()) {
m_edge = 0; m_edge = Edge();
} else { } else {
m_edge = new Edge((*m_vertex_it).m_data, m_edge = Edge((*m_vertex_it).m_data, (*m_edge_it).m_destination, (*m_edge_it).m_weight);
(*m_edge_it).m_destination,
(*m_edge_it).m_weight);
} }
} }

@ -73,9 +73,9 @@ int main()
++g_it, ++v_it) { ++g_it, ++v_it) {
std::cout << "victor: " std::cout << "victor: "
// << *g_it << " " // << *g_it << " "
<< **g_it << *g_it
<< std::endl; << 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 != g2.vertex_end();
++g_it, ++v_it) { ++g_it, ++v_it) {
std::cout << "vector: " std::cout << "vector: "
// << *g_it << " " << *g_it
<< **g_it
<< std::endl; << std::endl;
assert(**g_it == *v_it); assert(*g_it == *v_it);
} }
@ -135,9 +134,7 @@ int main()
Graph<int>::pointer source = (*edge_it).getSource(); Graph<int>::pointer source = (*edge_it).getSource();
Graph<int>::pointer destination = (*edge_it).getDestination(); Graph<int>::pointer destination = (*edge_it).getDestination();
std::cout << "edge: " std::cout << "edge: "
// << source << " "
<< *source << " " << *source << " "
// << destination << " "
<< *destination << " " << *destination << " "
<< (*edge_it).getWeight() << std::endl; << (*edge_it).getWeight() << std::endl;
} }

Loading…
Cancel
Save