Skip to content

Commit 9bed95c

Browse files
Merge pull request #424 from murraycu/develop-murrayc-examples-modern-cpp-attempt2d
examples: C++11: Use std::begin() and std::end()
2 parents 4792e04 + d93f67d commit 9bed95c

7 files changed

+18
-38
lines changed

example/bellman-ford-internet.cpp

+3-4
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,18 @@ int main()
2525
H,
2626
n_vertices
2727
};
28-
const int n_edges = 11;
2928
typedef std::pair< int, int > Edge;
3029

3130
// The list of connections between routers stored in an array.
32-
Edge edges[] = { Edge(A, B), Edge(A, C), Edge(B, D), Edge(B, E), Edge(C, E),
33-
Edge(C, F), Edge(D, H), Edge(D, E), Edge(E, H), Edge(F, G),
31+
Edge edges[] = { Edge(A, B), Edge(A, C), Edge(B, D), Edge(B, E),
32+
Edge(C, E), Edge(C, F), Edge(D, H), Edge(D, E), Edge(E, H), Edge(F, G),
3433
Edge(G, H) };
3534

3635
// Specify the graph type and declare a graph object
3736
typedef edge_list< Edge*, Edge, std::ptrdiff_t,
3837
std::random_access_iterator_tag >
3938
Graph;
40-
Graph g(edges, edges + n_edges);
39+
Graph g(std::begin(edges), std::end(edges));
4140

4241
// The transmission delay values for each edge.
4342
float delay[] = { 5.0, 1.0, 1.3, 3.0, 10.0, 2.0, 6.3, 0.4, 1.3, 1.2, 0.5 };

example/min_max_paths.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ int main(int, char*[])
5252
const char name[] = "abcdef";
5353

5454
const int num_nodes = 6;
55-
E edges[] = { E(0, 2), E(1, 1), E(1, 3), E(1, 4), E(2, 1), E(2, 3), E(3, 4),
56-
E(4, 0), E(4, 1) };
55+
const auto edges = { E(0, 2), E(1, 1), E(1, 3), E(1, 4), E(2, 1), E(2, 3),
56+
E(3, 4), E(4, 0), E(4, 1) };
5757
int weights[] = { 1, 2, 1, 2, 7, 3, 1, 1, 1 };
5858
const int n_edges = sizeof(edges) / sizeof(E);
5959
#if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
@@ -68,8 +68,8 @@ int main(int, char*[])
6868
weightmap[e] = weights[j];
6969
}
7070
#else
71-
Graph G(edges, edges + n_edges, weights, num_nodes);
72-
auto weightmap = get(edge_weight, G);
71+
Graph G(std::begin(edges), std::end(edges), weights, num_nodes);
72+
auto = get(edge_weight, G);
7373
#endif
7474

7575
std::vector< Vertex > p(num_vertices(G));

example/prim-example.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ int main()
1818
Graph;
1919
typedef std::pair< int, int > E;
2020
const int num_nodes = 5;
21-
E edges[]
21+
const auto edges
2222
= { E(0, 2), E(1, 3), E(1, 4), E(2, 1), E(2, 3), E(3, 4), E(4, 0) };
2323
int weights[] = { 1, 1, 2, 7, 3, 1, 1 };
2424
#if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
@@ -32,7 +32,7 @@ int main()
3232
weightmap[e] = weights[j];
3333
}
3434
#else
35-
Graph g(edges, edges + sizeof(edges) / sizeof(E), weights, num_nodes);
35+
Graph g(std::begin(edges), std::end(edges), weights, num_nodes);
3636
#endif
3737
std::vector< graph_traits< Graph >::vertex_descriptor > p(num_vertices(g));
3838

example/remove_edge_if_dir.cpp

+2-9
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,10 @@ typedef adjacency_list< vecS, vecS, directedS > Graph;
4141
int main()
4242
{
4343
typedef std::pair< std::size_t, std::size_t > Edge;
44-
Edge edges[6] = { Edge(0, 3), Edge(0, 2), Edge(0, 3), Edge(1, 3),
44+
const auto edges = { Edge(0, 3), Edge(0, 2), Edge(0, 3), Edge(1, 3),
4545
Edge(2, 0), Edge(3, 2) };
4646

47-
#if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
48-
// VC++ can't handle iterator constructor
49-
Graph g(4);
50-
for (std::size_t j = 0; j < 6; ++j)
51-
add_edge(edges[j].first, edges[j].second, g);
52-
#else
53-
Graph g(edges, edges + 6, 4);
54-
#endif
47+
Graph g(std::begin(edges), std::end(edges), 4);
5548

5649
std::cout << "original graph:" << std::endl;
5750
print_graph(g, get(vertex_index, g));

example/stoer_wagner.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ int main()
4545

4646
// construct the graph object. 8 is the number of vertices, which are
4747
// numbered from 0 through 7, and 16 is the number of edges.
48-
undirected_graph g(edges, edges + 16, ws, 8, 16);
48+
undirected_graph g(std::begin(edges), std::end(edges), ws, 8, 16);
4949

5050
// define a property map, `parities`, that will store a boolean value for
5151
// each vertex. Vertices that have the same parity after

example/topo_sort.cpp

+3-9
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,10 @@ int main(int, char*[])
4343
Graph;
4444

4545
typedef boost::graph_traits< Graph >::vertex_descriptor Vertex;
46-
Pair edges[6] = { Pair(0, 1), Pair(2, 4), Pair(2, 5), Pair(0, 3),
46+
const auto edges = { Pair(0, 1), Pair(2, 4), Pair(2, 5), Pair(0, 3),
4747
Pair(1, 4), Pair(4, 3) };
48-
#if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
49-
// VC++ can't handle the iterator constructor
50-
Graph G(6);
51-
for (std::size_t j = 0; j < 6; ++j)
52-
add_edge(edges[j].first, edges[j].second, G);
53-
#else
54-
Graph G(edges, edges + 6, 6);
55-
#endif
48+
49+
Graph G(std::begin(edges), std::end(edges), 6 /* vertices count */);
5650

5751
auto id = get(vertex_index, G);
5852

example/visitor.cpp

+3-9
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,9 @@ int main(int, char*[])
7171

7272
typedef adjacency_list<> Graph;
7373
typedef std::pair< int, int > E;
74-
E edges[] = { E(0, 2), E(1, 1), E(1, 3), E(2, 1), E(2, 3), E(3, 1), E(3, 4),
75-
E(4, 0), E(4, 1) };
76-
#if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
77-
Graph G(5);
78-
for (std::size_t j = 0; j < sizeof(edges) / sizeof(E); ++j)
79-
add_edge(edges[j].first, edges[j].second, G);
80-
#else
81-
Graph G(edges, edges + sizeof(edges) / sizeof(E), 5);
82-
#endif
74+
const auto edges = { E(0, 2), E(1, 1), E(1, 3), E(2, 1), E(2, 3), E(3, 1),
75+
E(3, 4), E(4, 0), E(4, 1) };
76+
Graph G(std::begin(edges), std::end(edges), 5);
8377

8478
typedef boost::graph_traits< Graph >::vertices_size_type size_type;
8579

0 commit comments

Comments
 (0)