Skip to content

Commit bfaddb5

Browse files
committed
examples: C++11: Use auto.
1 parent 538a915 commit bfaddb5

108 files changed

Lines changed: 286 additions & 389 deletions

File tree

Some content is hidden

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

example/accum-compile-times.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,10 @@ main()
6969
file_dep_graph2 g(input_begin, input_end, n_vertices);
7070
#endif
7171

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

7977
std::ifstream name_in("makefile-target-names.dat");
8078
std::ifstream compile_cost_in("target-compile-costs.dat");

example/actor_clustering.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,21 +50,21 @@ void load_actor_graph(std::istream& in, ActorGraph& g)
5050
// Map from the actor numbers on this line to the actor vertices
5151
typedef tokenizer<char_separator<char> > Tok;
5252
Tok tok(line, char_separator<char>(" "));
53-
for (Tok::iterator id = tok.begin(); id != tok.end(); ++id) {
54-
int actor_id = lexical_cast<int>(*id);
55-
std::map<int, Vertex>::iterator v = actors.find(actor_id);
53+
for (const auto& id : tok) {
54+
auto actor_id = lexical_cast<int>(id);
55+
auto v = actors.find(actor_id);
5656
if (v == actors.end()) {
57-
Vertex new_vertex = add_vertex(Actor(actor_id), g);
57+
auto new_vertex = add_vertex(Actor(actor_id), g);
5858
actors[actor_id] = new_vertex;
5959
actors_in_movie.push_back(new_vertex);
6060
} else {
6161
actors_in_movie.push_back(v->second);
6262
}
6363
}
6464

65-
for (std::vector<Vertex>::iterator i = actors_in_movie.begin();
65+
for (auto i = actors_in_movie.begin();
6666
i != actors_in_movie.end(); ++i) {
67-
for (std::vector<Vertex>::iterator j = i + 1;
67+
for (auto j = i + 1;
6868
j != actors_in_movie.end(); ++j) {
6969
if (!edge(*i, *j, g).second) add_edge(*i, *j, g);
7070
}
@@ -78,14 +78,12 @@ write_pajek_graph(std::ostream& out, const Graph& g,
7878
VertexIndexMap vertex_index, VertexNameMap vertex_name)
7979
{
8080
out << "*Vertices " << num_vertices(g) << '\n';
81-
typedef typename graph_traits<Graph>::vertex_iterator vertex_iterator;
82-
for (vertex_iterator v = vertices(g).first; v != vertices(g).second; ++v) {
81+
for (auto v = vertices(g).first; v != vertices(g).second; ++v) {
8382
out << get(vertex_index, *v)+1 << " \"" << get(vertex_name, *v) << "\"\n";
8483
}
8584

8685
out << "*Edges\n";
87-
typedef typename graph_traits<Graph>::edge_iterator edge_iterator;
88-
for (edge_iterator e = edges(g).first; e != edges(g).second; ++e) {
86+
for (auto e = edges(g).first; e != edges(g).second; ++e) {
8987
out << get(vertex_index, source(*e, g))+1 << ' '
9088
<< get(vertex_index, target(*e, g))+1 << " 1.0\n"; // HACK!
9189
}

example/adjacency_list.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,8 @@ int main(int , char* [])
5959
const int V = 5;
6060
Graph g(V);
6161

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

6765
boost::graph_traits<Graph>::vertex_iterator vi, viend;
6866
int vnum = 0;

example/astar-cities.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ class distance_heuristic : public astar_heuristic<Graph, CostType>
8181
: m_location(l), m_goal(goal) {}
8282
CostType operator()(Vertex u)
8383
{
84-
CostType dx = m_location[m_goal].x - m_location[u].x;
85-
CostType dy = m_location[m_goal].y - m_location[u].y;
84+
auto dx = m_location[m_goal].x - m_location[u].x;
85+
auto dy = m_location[m_goal].y - m_location[u].y;
8686
return ::sqrt(dx * dx + dy * dy);
8787
}
8888
private:
@@ -160,7 +160,7 @@ int main(int argc, char **argv)
160160

161161
// create graph
162162
mygraph_t g(N);
163-
WeightMap weightmap = get(edge_weight, g);
163+
auto weightmap = get(edge_weight, g);
164164
for(std::size_t j = 0; j < num_edges; ++j) {
165165
edge_descriptor e; bool inserted;
166166
boost::tie(e, inserted) = add_edge(edge_array[j].first,
@@ -171,8 +171,8 @@ int main(int argc, char **argv)
171171

172172
// pick random start/goal
173173
boost::mt19937 gen(time(0));
174-
vertex start = random_vertex(g, gen);
175-
vertex goal = random_vertex(g, gen);
174+
auto start = random_vertex(g, gen);
175+
auto goal = random_vertex(g, gen);
176176

177177

178178
cout << "Start vertex: " << name[start] << endl;
@@ -202,14 +202,14 @@ int main(int argc, char **argv)
202202

203203
} catch(found_goal fg) { // found a path to the goal
204204
list<vertex> shortest_path;
205-
for(vertex v = goal;; v = p[v]) {
205+
for(auto v = goal;; v = p[v]) {
206206
shortest_path.push_front(v);
207207
if(p[v] == v)
208208
break;
209209
}
210210
cout << "Shortest path from " << name[start] << " to "
211211
<< name[goal] << ": ";
212-
list<vertex>::iterator spi = shortest_path.begin();
212+
auto spi = shortest_path.begin();
213213
cout << name[start];
214214
for(++spi; spi != shortest_path.end(); ++spi)
215215
cout << " -> " << name[*spi];

example/astar_maze.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,8 @@ bool maze::solve() {
178178
dist_map distance;
179179
boost::associative_property_map<dist_map> dist_pmap(distance);
180180

181-
vertex_descriptor s = source();
182-
vertex_descriptor g = goal();
181+
auto s = source();
182+
auto g = goal();
183183
euclidean_heuristic heuristic(g);
184184
astar_goal_visitor visitor(g);
185185

@@ -192,7 +192,7 @@ bool maze::solve() {
192192
} catch(found_goal fg) {
193193
// Walk backwards from the goal through the predecessor chain adding
194194
// vertices to the solution path.
195-
for (vertex_descriptor u = g; u != s; u = predecessor[u])
195+
for (auto u = g; u != s; u = predecessor[u])
196196
m_solution.insert(u);
197197
m_solution.insert(s);
198198
m_solution_length = distance[g];
@@ -256,9 +256,9 @@ std::size_t random_int(std::size_t a, std::size_t b) {
256256
// Generate a maze with a random assignment of barriers.
257257
maze random_maze(std::size_t x, std::size_t y) {
258258
maze m(x, y);
259-
vertices_size_type n = num_vertices(m.m_grid);
260-
vertex_descriptor s = m.source();
261-
vertex_descriptor g = m.goal();
259+
auto n = num_vertices(m.m_grid);
260+
auto s = m.source();
261+
auto g = m.goal();
262262
// One quarter of the cells in the maze should be barriers.
263263
int barriers = n/4;
264264
while (barriers > 0) {
@@ -267,7 +267,7 @@ maze random_maze(std::size_t x, std::size_t y) {
267267
// Walls range up to one quarter the dimension length in this direction.
268268
vertices_size_type wall = random_int(1, m.length(direction)/4);
269269
// Create the wall while decrementing the total barrier count.
270-
vertex_descriptor u = vertex(random_int(0, n-1), m.m_grid);
270+
auto u = vertex(random_int(0, n-1), m.m_grid);
271271
while (wall) {
272272
// Start and goal spaces should never be barriers.
273273
if (u != s && u != g) {
@@ -277,7 +277,7 @@ maze random_maze(std::size_t x, std::size_t y) {
277277
barriers--;
278278
}
279279
}
280-
vertex_descriptor v = m.m_grid.next(u, direction);
280+
auto v = m.m_grid.next(u, direction);
281281
// Stop creating this wall if we reached the maze's edge.
282282
if (u == v)
283283
break;

example/bellman-example.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ struct edge_writer
2727
void operator() (std::ostream & out, const Edge & e) const
2828
{
2929
out << "[label=\"" << get(edge_weight, m_g, e) << "\"";
30-
typename graph_traits < Graph >::vertex_descriptor
31-
u = source(e, m_g), v = target(e, m_g);
30+
auto u = source(e, m_g), v = target(e, m_g);
3231
if (m_parent[v] == u)
3332
out << ", color=\"black\"";
3433
else
@@ -71,8 +70,7 @@ main()
7170
Graph g(edge_array, edge_array + n_edges, N);
7271
#endif
7372
graph_traits < Graph >::edge_iterator ei, ei_end;
74-
property_map<Graph, int EdgeProperties::*>::type
75-
weight_pmap = get(&EdgeProperties::weight, g);
73+
auto weight_pmap = get(&EdgeProperties::weight, g);
7674
int i = 0;
7775
for (boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei, ++i)
7876
weight_pmap[*ei] = weight[i];
@@ -109,9 +107,8 @@ main()
109107

110108
{
111109
for (boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei) {
112-
graph_traits < Graph >::edge_descriptor e = *ei;
113-
graph_traits < Graph >::vertex_descriptor
114-
u = source(e, g), v = target(e, g);
110+
auto e = *ei;
111+
auto u = source(e, g), v = target(e, g);
115112
// VC++ doesn't like the 3-argument get function, so here
116113
// we workaround by using 2-nested get()'s.
117114
dot_file << name[u] << " -> " << name[v]

example/bfs-example2.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ main()
6969

7070
Size time = 0;
7171
typedef property_map<graph_t, std::size_t VertexProps::*>::type dtime_map_t;
72-
dtime_map_t dtime_map = get(&VertexProps::discover_time, g);
72+
auto dtime_map = get(&VertexProps::discover_time, g);
7373
bfs_time_visitor < dtime_map_t > vis(dtime_map, time);
7474
breadth_first_search(g, vertex(s, g), color_map(get(&VertexProps::color, g)).
7575
visitor(vis));

example/bfs-name-printer.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,15 @@ void
1717
build_router_network(Graph & g, VertexNameMap name_map,
1818
TransDelayMap delay_map)
1919
{
20-
typename graph_traits < Graph >::vertex_descriptor a, b, c, d, e;
21-
a = add_vertex(g);
20+
auto a = add_vertex(g);
2221
name_map[a] = 'a';
23-
b = add_vertex(g);
22+
auto b = add_vertex(g);
2423
name_map[b] = 'b';
25-
c = add_vertex(g);
24+
auto c = add_vertex(g);
2625
name_map[c] = 'c';
27-
d = add_vertex(g);
26+
auto d = add_vertex(g);
2827
name_map[d] = 'd';
29-
e = add_vertex(g);
28+
auto e = add_vertex(g);
3029
name_map[e] = 'e';
3130

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

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

8584
build_router_network(g, name_map, delay_map);
8685

8786
typedef property_map<graph_t, char VP::*>::type VertexNameMap;
88-
graph_traits<graph_t>::vertex_descriptor a = *vertices(g).first;
87+
auto a = *vertices(g).first;
8988
bfs_name_printer<VertexNameMap> vis(name_map);
9089
std::cout << "BFS vertex discover order: ";
9190
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
135135
(G, s,

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
129129
(G, s,

0 commit comments

Comments
 (0)