Changeset 875:fda944f15ca7 in lemon-0.x
- Timestamp:
- 09/16/04 21:51:28 (20 years ago)
- Branch:
- default
- Phase:
- public
- Convert:
- svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@1178
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/graphs.dox
r873 r875 61 61 \todo Some cross-refs are wrong. 62 62 63 \bug This file must be updated accordig to the new style iterators.64 65 63 The graph structures itself can not store data attached 66 64 to the edges and nodes. However they all provide … … 98 96 g.addNode(); 99 97 100 for (NodeIt i(g); g.valid(i); g.next(i))101 for (NodeIt j(g); g.valid(j); g.next(j))98 for (NodeIt i(g); i!=INVALID; ++i) 99 for (NodeIt j(g); j!=INVALID; ++j) 102 100 if (i != j) g.addEdge(i, j); 103 101 \endcode … … 108 106 \code 109 107 std::cout << "Nodes:"; 110 for (NodeIt i(g); g.valid(i); g.next(i))108 for (NodeIt i(g); i!=INVALID; ++i) 111 109 std::cout << " " << g.id(i); 112 110 std::cout << std::endl; … … 114 112 115 113 Here we iterate through all nodes of the graph. We use a constructor of the 116 node iterator to initialize it to the first node. The next member function is 117 used to step to the next node, and valid is used to check if we have passed the 118 last one. 114 node iterator to initialize it to the first node. The operator++ is used to 115 step to the next node. Using operator++ on the iterator pointing to the last 116 node invalidates the iterator i.e. sets its value to 117 \ref hugo::INVALID "INVALID". This is what we exploit in the stop condition. 119 118 120 \code 121 std::cout << "Nodes:"; 122 NodeIt n; 123 for (g.first(n); n != INVALID; g.next(n)) 124 std::cout << " " << g.id(n); 125 std::cout << std::endl; 126 \endcode 127 128 Here you can see an alternative way to iterate through all nodes. Here we use a 129 member function of the graph to initialize the node iterator to the first node 130 of the graph. Using next on the iterator pointing to the last node invalidates 131 the iterator i.e. sets its value to INVALID. Checking for this value is 132 equivalent to using the valid member function. 133 134 Both of the previous code fragments print out the same: 119 The previous code fragment prints out the following: 135 120 136 121 \code … … 140 125 \code 141 126 std::cout << "Edges:"; 142 for (EdgeIt i(g); g.valid(i); g.next(i))127 for (EdgeIt i(g); i!=INVALID; ++i) 143 128 std::cout << " (" << g.id(g.tail(i)) << "," << g.id(g.head(i)) << ")"; 144 129 std::cout << std::endl; … … 156 141 157 142 std::cout << "Out-edges of node " << g.id(first_node) << ":"; 158 for (OutEdgeIt i(g, first_node); g.valid(i); g.next(i))143 for (OutEdgeIt i(g, first_node); i!=INVALID; ++i) 159 144 std::cout << " (" << g.id(g.tail(i)) << "," << g.id(g.head(i)) << ")"; 160 145 std::cout << std::endl; 161 146 162 147 std::cout << "In-edges of node " << g.id(first_node) << ":"; 163 for (InEdgeIt i(g, first_node); g.valid(i); g.next(i))148 for (InEdgeIt i(g, first_node); i!=INVALID; ++i) 164 149 std::cout << " (" << g.id(g.tail(i)) << "," << g.id(g.head(i)) << ")"; 165 150 std::cout << std::endl; … … 177 162 Graph::EdgeMap<int> m(g); 178 163 179 for (EdgeIt e(g); g.valid(e); g.next(e))164 for (EdgeIt e(g); e!=INVALID; ++e) 180 165 m.set(e, 10 - g.id(e)); 181 166 182 167 std::cout << "Id Edge Value" << std::endl; 183 for (EdgeIt e(g); g.valid(e); g.next(e))168 for (EdgeIt e(g); e!=INVALID; ++e) 184 169 std::cout << g.id(e) << " (" << g.id(g.tail(e)) << "," << g.id(g.head(e)) 185 170 << ") " << m[e] << std::endl;
Note: See TracChangeset
for help on using the changeset viewer.