Skip to content

Commit cbae979

Browse files
Merge pull request #421 from murraycu/develop-murrayc-examples-modern-cpp-attempt2
examples: C++11: Some use of range-based for loops
2 parents 9bed95c + 6ebdc2c commit cbae979

7 files changed

+29
-31
lines changed

example/cuthill_mckee_ordering.cpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ int main(int, char*[])
5757
Pair(6, 7) }; // g-h
5858

5959
Graph G(10);
60-
for (int i = 0; i < 14; ++i)
61-
add_edge(edges[i].first, edges[i].second, G);
60+
for (auto const& edge : edges)
61+
add_edge(edge.first, edge.second, G);
6262

6363
graph_traits< Graph >::vertex_iterator ui, ui_end;
6464

@@ -79,8 +79,8 @@ int main(int, char*[])
7979
get(vertex_degree, G));
8080
cout << "Reverse Cuthill-McKee ordering starting at: " << s << endl;
8181
cout << " ";
82-
for (auto i = inv_perm.begin(); i != inv_perm.end(); ++i)
83-
cout << index_map[*i] << " ";
82+
for (auto const& vertex : inv_perm)
83+
cout << index_map[vertex] << " ";
8484
cout << endl;
8585

8686
for (size_type c = 0; c != inv_perm.size(); ++c)
@@ -98,8 +98,8 @@ int main(int, char*[])
9898
get(vertex_degree, G));
9999
cout << "Reverse Cuthill-McKee ordering starting at: " << s << endl;
100100
cout << " ";
101-
for (auto i = inv_perm.begin(); i != inv_perm.end(); ++i)
102-
cout << index_map[*i] << " ";
101+
for (auto const& vertex : inv_perm)
102+
cout << index_map[vertex] << " ";
103103
cout << endl;
104104

105105
for (size_type c = 0; c != inv_perm.size(); ++c)
@@ -118,8 +118,8 @@ int main(int, char*[])
118118

119119
cout << "Reverse Cuthill-McKee ordering:" << endl;
120120
cout << " ";
121-
for (auto i = inv_perm.begin(); i != inv_perm.end(); ++i)
122-
cout << index_map[*i] << " ";
121+
for (auto const& vertex : inv_perm)
122+
cout << index_map[vertex] << " ";
123123
cout << endl;
124124

125125
for (size_type c = 0; c != inv_perm.size(); ++c)

example/edge-connectivity.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,13 @@ typename graph_traits< VertexListGraph >::degree_size_type edge_connectivity(
136136
}
137137

138138
std::vector< bool > in_S_star(num_vertices(g), false);
139-
for (auto si = S_star.begin(); si != S_star.end(); ++si)
140-
in_S_star[*si] = true;
139+
for (auto const& vertex : S_star.begin())
140+
in_S_star[vertex] = true;
141141
degree_size_type c = 0;
142-
for (auto si = S_star.begin(); si != S_star.end(); ++si)
142+
for (auto const& vertex : S_star.begin())
143143
{
144144
typename graph_traits< VertexListGraph >::out_edge_iterator ei, ei_end;
145-
for (boost::tie(ei, ei_end) = out_edges(*si, g); ei != ei_end; ++ei)
145+
for (boost::tie(ei, ei_end) = out_edges(vertex, g); ei != ei_end; ++ei)
146146
if (!in_S_star[target(*ei, g)])
147147
{
148148
*disconnecting_set++ = *ei;
@@ -170,9 +170,9 @@ int main()
170170
auto attr_map = get(vertex_attribute, g);
171171

172172
std::cout << "The disconnecting set is {";
173-
for (auto i = disconnecting_set.begin(); i != disconnecting_set.end(); ++i)
174-
std::cout << "(" << attr_map[source(*i, g)]["label"] << ","
175-
<< attr_map[target(*i, g)]["label"] << ") ";
173+
for (auto const& edge : disconnecting_set)
174+
std::cout << "(" << attr_map[source(edge, g)]["label"] << ","
175+
<< attr_map[target(edge, g)]["label"] << ") ";
176176
std::cout << "}." << std::endl;
177177
return EXIT_SUCCESS;
178178
}

example/king_ordering.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ int main(int, char*[])
9898
get(vertex_degree, G), get(vertex_index, G));
9999
cout << "King ordering starting at: " << s << endl;
100100
cout << " ";
101-
for (auto i = inv_perm.begin(); i != inv_perm.end(); ++i)
102-
cout << index_map[*i] << " ";
101+
for (auto const& vertex : inv_perm)
102+
cout << index_map[vertex] << " ";
103103
cout << endl;
104104

105105
for (size_type c = 0; c != inv_perm.size(); ++c)
@@ -118,8 +118,8 @@ int main(int, char*[])
118118

119119
cout << "King ordering:" << endl;
120120
cout << " ";
121-
for (auto i = inv_perm.begin(); i != inv_perm.end(); ++i)
122-
cout << index_map[*i] << " ";
121+
for (auto const& vertex : inv_perm)
122+
cout << index_map[vertex] << " ";
123123
cout << endl;
124124

125125
for (size_type c = 0; c != inv_perm.size(); ++c)

example/kruskal-telephone.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,14 @@ int main()
4646

4747
auto weight = get(edge_weight, g);
4848
int total_weight = 0;
49-
for (size_type e = 0; e < mst.size(); ++e)
50-
total_weight += get(weight, mst[e]);
49+
for (auto const& edge : mst)
50+
total_weight += get(weight, edge);
5151
std::cout << "total weight: " << total_weight << std::endl;
5252

5353
typedef graph_traits< Graph >::vertex_descriptor Vertex;
54-
for (size_type i = 0; i < mst.size(); ++i)
54+
for (auto const& edge : mst)
5555
{
56-
auto u = source(mst[i], g), v = target(mst[i], g);
56+
auto u = source(edge, g), v = target(edge, g);
5757
edge_attr_map[edge(u, v, g_dot).first]["color"] = "black";
5858
}
5959
std::ofstream out("figs/telephone-mst-kruskal.dot");

example/matching_example.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ int main()
7474

7575
std::cout << "In the following graph:" << std::endl << std::endl;
7676

77-
for (auto itr = ascii_graph.begin(); itr != ascii_graph.end(); ++itr)
78-
std::cout << *itr << std::endl;
77+
for (auto const& str : ascii_graph)
78+
std::cout << str << std::endl;
7979

8080
std::cout << std::endl
8181
<< "Found a matching of size " << matching_size(g, &mate[0])

example/topo-sort1.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,8 @@ int main()
2828
topological_sort(g, std::front_inserter(topo_order),
2929
vertex_index_map(identity_property_map()));
3030

31-
int n = 1;
32-
for (auto i = topo_order.begin(); i != topo_order.end(); ++i, ++n)
33-
std::cout << tasks[*i] << std::endl;
31+
for (auto const& vertex : topo_order)
32+
std::cout << tasks[vertex] << std::endl;
3433

3534
return EXIT_SUCCESS;
3635
}

example/topo-sort2.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,8 @@ int main()
3535
topological_sort(g, std::front_inserter(topo_order),
3636
vertex_index_map(identity_property_map()));
3737

38-
int n = 1;
39-
for (auto i = topo_order.begin(); i != topo_order.end(); ++i, ++n)
40-
std::cout << tasks[*i] << std::endl;
38+
for (auto const& vertex : topo_order)
39+
std::cout << tasks[vertex] << std::endl;
4140

4241
return EXIT_SUCCESS;
4342
}

0 commit comments

Comments
 (0)