Skip to content

Commit 9ed5d90

Browse files
committed
examples: C++11: Use auto
1 parent ce1daeb commit 9ed5d90

File tree

107 files changed

+284
-419
lines changed

Some content is hidden

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

107 files changed

+284
-419
lines changed

example/accum-compile-times.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,8 @@ 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);
8177

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

example/actor_clustering.cpp

Lines changed: 8 additions & 12 deletions
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

Lines changed: 2 additions & 4 deletions
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

Lines changed: 8 additions & 8 deletions
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

Lines changed: 8 additions & 8 deletions
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

Lines changed: 4 additions & 7 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 8 additions & 9 deletions
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);
19+
auto a = add_vertex(g);
2120
name_map[a] = 'a';
22-
b = add_vertex(g);
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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(

0 commit comments

Comments
 (0)