Skip to content

Commit 6b7f7b4

Browse files
committed
examples: C++11: Some use of range-based for loop.
1 parent bfaddb5 commit 6b7f7b4

7 files changed

Lines changed: 29 additions & 39 deletions

example/cuthill_mckee_ordering.cpp

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ int main(int , char* [])
5656
Pair(6,7) }; //g-h
5757

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

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

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

8685
for (size_type c = 0; c != inv_perm.size(); ++c)
@@ -96,9 +95,8 @@ int main(int , char* [])
9695
get(vertex_degree, G));
9796
cout << "Reverse Cuthill-McKee ordering starting at: " << s << endl;
9897
cout << " ";
99-
for (auto i=inv_perm.begin();
100-
i != inv_perm.end(); ++i)
101-
cout << index_map[*i] << " ";
98+
for (const auto& vertex : inv_perm)
99+
cout << index_map[vertex] << " ";
102100
cout << endl;
103101

104102
for (size_type c = 0; c != inv_perm.size(); ++c)
@@ -115,9 +113,8 @@ int main(int , char* [])
115113

116114
cout << "Reverse Cuthill-McKee ordering:" << endl;
117115
cout << " ";
118-
for (auto i=inv_perm.begin();
119-
i != inv_perm.end(); ++i)
120-
cout << index_map[*i] << " ";
116+
for (const auto& vertex : inv_perm)
117+
cout << index_map[vertex] << " ";
121118
cout << endl;
122119

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

example/edge-connectivity.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,12 @@ namespace boost
126126
}
127127

128128
std::vector < bool > in_S_star(num_vertices(g), false);
129-
for (auto si = S_star.begin(); si != S_star.end(); ++si)
130-
in_S_star[*si] = true;
129+
for (const auto& vertex : S_star.begin())
130+
in_S_star[vertex] = true;
131131
degree_size_type c = 0;
132-
for (auto si = S_star.begin(); si != S_star.end(); ++si) {
132+
for (const auto& vertex : S_star.begin()) {
133133
typename graph_traits < VertexListGraph >::out_edge_iterator ei, ei_end;
134-
for (boost::tie(ei, ei_end) = out_edges(*si, g); ei != ei_end; ++ei)
134+
for (boost::tie(ei, ei_end) = out_edges(vertex, g); ei != ei_end; ++ei)
135135
if (!in_S_star[target(*ei, g)]) {
136136
*disconnecting_set++ = *ei;
137137
++c;
@@ -159,11 +159,10 @@ main()
159159
auto attr_map = get(vertex_attribute, g);
160160

161161
std::cout << "The disconnecting set is {";
162-
for (auto i =
163-
disconnecting_set.begin(); i != disconnecting_set.end(); ++i)
162+
for (const auto& edge : disconnecting_set)
164163
std::
165-
cout << "(" << attr_map[source(*i, g)]["label"] << "," <<
166-
attr_map[target(*i, g)]["label"] << ") ";
164+
cout << "(" << attr_map[source(edge, g)]["label"] << "," <<
165+
attr_map[target(edge, g)]["label"] << ") ";
167166
std::cout << "}." << std::endl;
168167
return EXIT_SUCCESS;
169168
}

example/king_ordering.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,8 @@ int main(int , char* [])
9696
get(vertex_degree, G), get(vertex_index, G));
9797
cout << "King ordering starting at: " << s << endl;
9898
cout << " ";
99-
for (auto i=inv_perm.begin();
100-
i != inv_perm.end(); ++i)
101-
cout << index_map[*i] << " ";
99+
for (const auto& vertex : inv_perm)
100+
cout << index_map[vertex] << " ";
102101
cout << endl;
103102

104103
for (size_type c = 0; c != inv_perm.size(); ++c)
@@ -115,9 +114,8 @@ int main(int , char* [])
115114

116115
cout << "King ordering:" << endl;
117116
cout << " ";
118-
for (auto i=inv_perm.begin();
119-
i != inv_perm.end(); ++i)
120-
cout << index_map[*i] << " ";
117+
for (const auto& vertex : inv_perm)
118+
cout << index_map[vertex] << " ";
121119
cout << endl;
122120

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

example/kruskal-telephone.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ main()
3636

3737
auto weight = get(edge_weight, g);
3838
int total_weight = 0;
39-
for (size_type e = 0; e < mst.size(); ++e)
40-
total_weight += get(weight, mst[e]);
39+
for (const auto& edge : mst)
40+
total_weight += get(weight, edge);
4141
std::cout << "total weight: " << total_weight << std::endl;
4242

4343
typedef graph_traits < Graph >::vertex_descriptor Vertex;
44-
for (size_type i = 0; i < mst.size(); ++i) {
45-
auto u = source(mst[i], g), v = target(mst[i], g);
44+
for (const auto& edge : mst) {
45+
auto u = source(edge, g), v = target(edge, g);
4646
edge_attr_map[edge(u, v, g_dot).first]["color"] = "black";
4747
}
4848
std::ofstream out("figs/telephone-mst-kruskal.dot");

example/matching_example.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ int main()
7676

7777
std::cout << "In the following graph:" << std::endl << std::endl;
7878

79-
for (auto itr = ascii_graph.begin(); itr != ascii_graph.end(); ++itr)
80-
std::cout << *itr << std::endl;
79+
for (const auto& str : ascii_graph)
80+
std::cout << str << std::endl;
8181

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

example/topo-sort1.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,8 @@ main()
4141
topological_sort(g, std::front_inserter(topo_order),
4242
vertex_index_map(identity_property_map()));
4343

44-
int n = 1;
45-
for (auto i = topo_order.begin();
46-
i != topo_order.end(); ++i, ++n)
47-
std::cout << tasks[*i] << std::endl;
44+
for (const auto& vertex : topo_order)
45+
std::cout << tasks[vertex] << std::endl;
4846

4947
return EXIT_SUCCESS;
5048
}

example/topo-sort2.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,8 @@ main()
4141
topological_sort(g, std::front_inserter(topo_order),
4242
vertex_index_map(identity_property_map()));
4343

44-
int n = 1;
45-
for (auto i = topo_order.begin();
46-
i != topo_order.end(); ++i, ++n)
47-
std::cout << tasks[*i] << std::endl;
44+
for (const auto& vertex : topo_order)
45+
std::cout << tasks[vertex] << std::endl;
4846

4947
return EXIT_SUCCESS;
5048
}

0 commit comments

Comments
 (0)