Skip to content

Commit 2b1abb1

Browse files
committed
examples: C++11: Use auto
1 parent ce1daeb commit 2b1abb1

File tree

108 files changed

+779
-121
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

108 files changed

+779
-121
lines changed

example/accum-compile-times.cpp

+4-6
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,10 @@ int main(int argc, const char** argv)
7272
file_dep_graph2 g(input_begin, input_end, n_vertices);
7373
#endif
7474

75-
typedef property_map< file_dep_graph2, vertex_name_t >::type name_map_t;
76-
typedef property_map< file_dep_graph2, vertex_compile_cost_t >::type
77-
compile_cost_map_t;
78-
79-
name_map_t name_map = get(vertex_name, g);
80-
compile_cost_map_t compile_cost_map = get(vertex_compile_cost, g);
75+
auto name_map = get(vertex_name, g);
76+
auto compile_cost_map = get(vertex_compile_cost, g);
77+
auto distance_map = get(vertex_distance, g);
78+
auto color_map = get(vertex_color, g);
8179

8280
std::ifstream name_in(argc >= 3 ? argv[2] : "makefile-target-names.dat");
8381
std::ifstream compile_cost_in(

example/actor_clustering.cpp

+8-12
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@ void load_actor_graph(std::istream& in, ActorGraph& g)
5252
// Map from the actor numbers on this line to the actor vertices
5353
typedef tokenizer< char_separator< char > > Tok;
5454
Tok tok(line, char_separator< char >(" "));
55-
for (Tok::iterator id = tok.begin(); id != tok.end(); ++id)
55+
for (const auto& id : tok)
5656
{
57-
int actor_id = lexical_cast< int >(*id);
58-
std::map< int, Vertex >::iterator v = actors.find(actor_id);
57+
auto actor_id = lexical_cast< int >(id);
58+
auto v = actors.find(actor_id);
5959
if (v == actors.end())
6060
{
61-
Vertex new_vertex = add_vertex(Actor(actor_id), g);
61+
auto new_vertex = add_vertex(Actor(actor_id), g);
6262
actors[actor_id] = new_vertex;
6363
actors_in_movie.push_back(new_vertex);
6464
}
@@ -68,11 +68,9 @@ void load_actor_graph(std::istream& in, ActorGraph& g)
6868
}
6969
}
7070

71-
for (std::vector< Vertex >::iterator i = actors_in_movie.begin();
72-
i != actors_in_movie.end(); ++i)
71+
for (auto i = actors_in_movie.begin(); i != actors_in_movie.end(); ++i)
7372
{
74-
for (std::vector< Vertex >::iterator j = i + 1;
75-
j != actors_in_movie.end(); ++j)
73+
for (auto j = i + 1; j != actors_in_movie.end(); ++j)
7674
{
7775
if (!edge(*i, *j, g).second)
7876
add_edge(*i, *j, g);
@@ -86,16 +84,14 @@ std::ostream& write_pajek_graph(std::ostream& out, const Graph& g,
8684
VertexIndexMap vertex_index, VertexNameMap vertex_name)
8785
{
8886
out << "*Vertices " << num_vertices(g) << '\n';
89-
typedef typename graph_traits< Graph >::vertex_iterator vertex_iterator;
90-
for (vertex_iterator v = vertices(g).first; v != vertices(g).second; ++v)
87+
for (auto v = vertices(g).first; v != vertices(g).second; ++v)
9188
{
9289
out << get(vertex_index, *v) + 1 << " \"" << get(vertex_name, *v)
9390
<< "\"\n";
9491
}
9592

9693
out << "*Edges\n";
97-
typedef typename graph_traits< Graph >::edge_iterator edge_iterator;
98-
for (edge_iterator e = edges(g).first; e != edges(g).second; ++e)
94+
for (auto e = edges(g).first; e != edges(g).second; ++e)
9995
{
10096
out << get(vertex_index, source(*e, g)) + 1 << ' '
10197
<< get(vertex_index, target(*e, g)) + 1 << " 1.0\n"; // HACK!

example/adjacency_list.cpp

+2-4
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,8 @@ int main(int, char*[])
6161
const int V = 5;
6262
Graph g(V);
6363

64-
property_map< Graph, std::size_t VertexProperties::* >::type id
65-
= get(&VertexProperties::index, g);
66-
property_map< Graph, std::string EdgeProperties::* >::type name
67-
= get(&EdgeProperties::name, g);
64+
auto id = get(&VertexProperties::index, g);
65+
auto name = get(&EdgeProperties::name, g);
6866

6967
boost::graph_traits< Graph >::vertex_iterator vi, viend;
7068
int vnum = 0;

example/astar-cities.cpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ class distance_heuristic : public astar_heuristic< Graph, CostType >
8686
distance_heuristic(LocMap l, Vertex goal) : m_location(l), m_goal(goal) {}
8787
CostType operator()(Vertex u)
8888
{
89-
CostType dx = m_location[m_goal].x - m_location[u].x;
90-
CostType dy = m_location[m_goal].y - m_location[u].y;
89+
auto dx = m_location[m_goal].x - m_location[u].x;
90+
auto dy = m_location[m_goal].y - m_location[u].y;
9191
return ::sqrt(dx * dx + dy * dy);
9292
}
9393

@@ -174,7 +174,7 @@ int main(int argc, char** argv)
174174

175175
// create graph
176176
mygraph_t g(N);
177-
WeightMap weightmap = get(edge_weight, g);
177+
auto weightmap = get(edge_weight, g);
178178
for (std::size_t j = 0; j < num_edges; ++j)
179179
{
180180
edge_descriptor e;
@@ -185,9 +185,9 @@ int main(int argc, char** argv)
185185
}
186186

187187
// pick random start/goal
188-
boost::mt19937 gen(std::time(0));
189-
vertex start = random_vertex(g, gen);
190-
vertex goal = random_vertex(g, gen);
188+
boost::mt19937 gen(time(0));
189+
auto start = random_vertex(g, gen);
190+
auto goal = random_vertex(g, gen);
191191

192192
cout << "Start vertex: " << name[start] << endl;
193193
cout << "Goal vertex: " << name[goal] << endl;
@@ -215,15 +215,15 @@ int main(int argc, char** argv)
215215
catch (found_goal fg)
216216
{ // found a path to the goal
217217
list< vertex > shortest_path;
218-
for (vertex v = goal;; v = p[v])
218+
for (auto v = goal;; v = p[v])
219219
{
220220
shortest_path.push_front(v);
221221
if (p[v] == v)
222222
break;
223223
}
224224
cout << "Shortest path from " << name[start] << " to " << name[goal]
225225
<< ": ";
226-
list< vertex >::iterator spi = shortest_path.begin();
226+
auto spi = shortest_path.begin();
227227
cout << name[start];
228228
for (++spi; spi != shortest_path.end(); ++spi)
229229
cout << " -> " << name[*spi];

example/astar_maze.cpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,8 @@ bool maze::solve()
201201
dist_map distance;
202202
boost::associative_property_map< dist_map > dist_pmap(distance);
203203

204-
vertex_descriptor s = source();
205-
vertex_descriptor g = goal();
204+
auto s = source();
205+
auto g = goal();
206206
euclidean_heuristic heuristic(g);
207207
astar_goal_visitor visitor(g);
208208

@@ -218,7 +218,7 @@ bool maze::solve()
218218
{
219219
// Walk backwards from the goal through the predecessor chain adding
220220
// vertices to the solution path.
221-
for (vertex_descriptor u = g; u != s; u = predecessor[u])
221+
for (auto u = g; u != s; u = predecessor[u])
222222
m_solution.insert(u);
223223
m_solution.insert(s);
224224
m_solution_length = distance[g];
@@ -285,9 +285,9 @@ std::size_t random_int(std::size_t a, std::size_t b)
285285
// Generate a maze with a random assignment of barriers.
286286
void random_maze(maze& m)
287287
{
288-
vertices_size_type n = num_vertices(m.m_grid);
289-
vertex_descriptor s = m.source();
290-
vertex_descriptor g = m.goal();
288+
auto n = num_vertices(m.m_grid);
289+
auto s = m.source();
290+
auto g = m.goal();
291291
// One quarter of the cells in the maze should be barriers.
292292
int barriers = n / 4;
293293
while (barriers > 0)
@@ -297,7 +297,7 @@ void random_maze(maze& m)
297297
// Walls range up to one quarter the dimension length in this direction.
298298
vertices_size_type wall = random_int(1, m.length(direction) / 4);
299299
// Create the wall while decrementing the total barrier count.
300-
vertex_descriptor u = vertex(random_int(0, n - 1), m.m_grid);
300+
auto u = vertex(random_int(0, n - 1), m.m_grid);
301301
while (wall)
302302
{
303303
// Start and goal spaces should never be barriers.
@@ -310,7 +310,7 @@ void random_maze(maze& m)
310310
barriers--;
311311
}
312312
}
313-
vertex_descriptor v = m.m_grid.next(u, direction);
313+
auto v = m.m_grid.next(u, direction);
314314
// Stop creating this wall if we reached the maze's edge.
315315
if (u == v)
316316
break;

example/bellman-example.cpp

+4-7
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ template < typename Graph, typename ParentMap > struct edge_writer
2323
void operator()(std::ostream& out, const Edge& e) const
2424
{
2525
out << "[label=\"" << get(edge_weight, m_g, e) << "\"";
26-
typename graph_traits< Graph >::vertex_descriptor u = source(e, m_g),
27-
v = target(e, m_g);
26+
auto u = source(e, m_g), v = target(e, m_g);
2827
if (m_parent[v] == u)
2928
out << ", color=\"black\"";
3029
else
@@ -74,8 +73,7 @@ int main()
7473
Graph g(edge_array, edge_array + n_edges, N);
7574
#endif
7675
graph_traits< Graph >::edge_iterator ei, ei_end;
77-
property_map< Graph, int EdgeProperties::* >::type weight_pmap
78-
= get(&EdgeProperties::weight, g);
76+
auto weight_pmap = get(&EdgeProperties::weight, g);
7977
int i = 0;
8078
for (boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei, ++i)
8179
weight_pmap[*ei] = weight[i];
@@ -115,9 +113,8 @@ int main()
115113
{
116114
for (boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei)
117115
{
118-
graph_traits< Graph >::edge_descriptor e = *ei;
119-
graph_traits< Graph >::vertex_descriptor u = source(e, g),
120-
v = target(e, g);
116+
auto e = *ei;
117+
auto u = source(e, g), v = target(e, g);
121118
// VC++ doesn't like the 3-argument get function, so here
122119
// we workaround by using 2-nested get()'s.
123120
dot_file << name[u] << " -> " << name[v] << "[label=\""

example/bfs-example2.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ int main()
8181
Size time = 0;
8282
typedef property_map< graph_t, std::size_t VertexProps::* >::type
8383
dtime_map_t;
84-
dtime_map_t dtime_map = get(&VertexProps::discover_time, g);
84+
auto dtime_map = get(&VertexProps::discover_time, g);
8585
bfs_time_visitor< dtime_map_t > vis(dtime_map, time);
8686
breadth_first_search(
8787
g, vertex(s, g), color_map(get(&VertexProps::color, g)).visitor(vis));

example/bfs-name-printer.cpp

+9-10
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,15 @@ template < typename Graph, typename VertexNameMap, typename TransDelayMap >
1616
void build_router_network(
1717
Graph& g, VertexNameMap name_map, TransDelayMap delay_map)
1818
{
19-
typename graph_traits< Graph >::vertex_descriptor a, b, c, d, e;
20-
a = add_vertex(g);
21-
name_map[a] = 'a';
22-
b = add_vertex(g);
19+
auto a = add_vertex(g);
20+
>>>>>>> 71108a43 (examples: C++11: Use auto.)
21+
auto b = add_vertex(g);
2322
name_map[b] = 'b';
24-
c = add_vertex(g);
23+
auto c = add_vertex(g);
2524
name_map[c] = 'c';
26-
d = add_vertex(g);
25+
auto d = add_vertex(g);
2726
name_map[d] = 'd';
28-
e = add_vertex(g);
27+
auto e = add_vertex(g);
2928
name_map[e] = 'e';
3029

3130
typename graph_traits< Graph >::edge_descriptor ed;
@@ -78,13 +77,13 @@ int main()
7877
typedef adjacency_list< listS, vecS, directedS, VP, EP > graph_t;
7978
graph_t g;
8079

81-
property_map< graph_t, char VP::* >::type name_map = get(&VP::name, g);
82-
property_map< graph_t, double EP::* >::type delay_map = get(&EP::weight, g);
80+
auto name_map = get(&VP::name, g);
81+
auto delay_map = get(&EP::weight, g);
8382

8483
build_router_network(g, name_map, delay_map);
8584

8685
typedef property_map< graph_t, char VP::* >::type VertexNameMap;
87-
graph_traits< graph_t >::vertex_descriptor a = *vertices(g).first;
86+
auto a = *vertices(g).first;
8887
bfs_name_printer< VertexNameMap > vis(name_map);
8988
std::cout << "BFS vertex discover order: ";
9089
breadth_first_search(g, a, visitor(vis));

example/bfs.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ int main(int, char*[])
129129
std::fill_n(d, 5, 0);
130130

131131
// The source vertex
132-
Vertex s = *(boost::vertices(G).first);
132+
auto s = *(boost::vertices(G).first);
133133
p[s] = s;
134134
boost::breadth_first_search(G, s,
135135
boost::visitor(boost::make_bfs_visitor(

example/bfs_neighbor.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ int main(int, char*[])
123123
std::fill_n(d, 5, 0);
124124

125125
// The source vertex
126-
Vertex s = *(boost::vertices(G).first);
126+
auto s = *(boost::vertices(G).first);
127127
p[s] = s;
128128
boost::neighbor_breadth_first_search(G, s,
129129
boost::visitor(boost::make_neighbor_bfs_visitor(

example/biconnected_components.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,9 @@ int main()
4545
add_edge(6, 7, g);
4646
add_edge(7, 8, g);
4747

48-
property_map< graph_t, edge_component_t >::type component
49-
= get(edge_component, g);
48+
auto component = get(edge_component, g);
5049

51-
std::size_t num_comps = biconnected_components(g, component);
50+
auto num_comps = biconnected_components(g, component);
5251
std::cerr << "Found " << num_comps << " biconnected components.\n";
5352

5453
std::vector< vertex_t > art_points;

example/boost_web_graph.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,8 @@ class calc_distance_visitor : public boost::bfs_visitor<>
2929
void tree_edge(
3030
typename boost::graph_traits< Graph >::edge_descriptor e, Graph& g)
3131
{
32-
typename boost::graph_traits< Graph >::vertex_descriptor u, v;
33-
u = boost::source(e, g);
34-
v = boost::target(e, g);
32+
auto u = boost::source(e, g);
33+
auto v = boost::target(e, g);
3534
distance[v] = distance[u] + 1;
3635
}
3736

@@ -95,9 +94,8 @@ int main(int argc, const char** argv)
9594
NameVertexMap name2vertex;
9695
Graph g;
9796

98-
typedef property_map< Graph, vertex_name_t >::type NameMap;
99-
NameMap node_name = get(vertex_name, g);
100-
property_map< Graph, edge_name_t >::type link_name = get(edge_name, g);
97+
auto node_name = get(vertex_name, g);
98+
auto link_name = get(edge_name, g);
10199

102100
//===========================================================================
103101
// Read the data file and construct the graph.
@@ -113,7 +111,7 @@ int main(int argc, const char** argv)
113111
bool inserted;
114112
Vertex u, v;
115113

116-
std::list< std::string >::iterator i = line_toks.begin();
114+
auto i = line_toks.begin();
117115

118116
boost::tie(pos, inserted)
119117
= name2vertex.insert(std::make_pair(*i, Vertex()));
@@ -210,6 +208,8 @@ int main(int argc, const char** argv)
210208
// the tree nodes in the order that we want to print out:
211209
// a directory-structure like format.
212210
std::vector< size_type > dfs_distances(num_vertices(g), 0);
211+
212+
using NameMap = property_map< Graph, vertex_name_t >::type;
213213
print_tree_visitor< NameMap, size_type* > tree_printer(
214214
node_name, &dfs_distances[0]);
215215
for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi)

example/boykov_kolmogorov-eg.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -85,17 +85,27 @@ int main()
8585
Graph;
8686

8787
Graph g;
88+
<<<<<<< HEAD
8889
property_map< Graph, edge_capacity_t >::type capacity
8990
= get(edge_capacity, g);
9091
property_map< Graph, edge_residual_capacity_t >::type residual_capacity
9192
= get(edge_residual_capacity, g);
9293
property_map< Graph, edge_reverse_t >::type rev = get(edge_reverse, g);
94+
=======
95+
auto capacity = get(edge_capacity, g);
96+
auto residual_capacity = get(edge_residual_capacity, g);
97+
auto rev = get(edge_reverse, g);
98+
>>>>>>> 71108a43 (examples: C++11: Use auto.)
9399
Traits::vertex_descriptor s, t;
94100
read_dimacs_max_flow(g, capacity, rev, s, t);
95101

96102
std::vector< default_color_type > color(num_vertices(g));
97103
std::vector< long > distance(num_vertices(g));
104+
<<<<<<< HEAD
98105
long flow = boykov_kolmogorov_max_flow(g, s, t);
106+
=======
107+
auto flow = boykov_kolmogorov_max_flow(g, s, t);
108+
>>>>>>> 71108a43 (examples: C++11: Use auto.)
99109

100110
std::cout << "c The total flow:" << std::endl;
101111
std::cout << "s " << flow << std::endl << std::endl;

example/bron_kerbosch_clique_number.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ int main(int argc, char* argv[])
2929
read_graph(g, cin);
3030

3131
// Use the Bron-Kerbosch algorithm to find all cliques, and
32-
size_t c = bron_kerbosch_clique_number(g);
32+
auto c = bron_kerbosch_clique_number(g);
3333
cout << "clique number: " << c << endl;
3434

3535
return 0;

0 commit comments

Comments
 (0)