9
9
// BFS
10
10
#include < queue>
11
11
12
+ template <typename T> class Graph ;
13
+
12
14
template <typename T>
13
15
struct Edge
14
16
{
@@ -29,6 +31,23 @@ struct Edge
29
31
}
30
32
};
31
33
34
+ template <typename T>
35
+ std::ostream& operator <<(std::ostream& os, const Graph<T>& G)
36
+ {
37
+ for (auto i = 1 ; i < G.vertices (); i++)
38
+ {
39
+ os << i << " :\t " ;
40
+
41
+ auto edges = G.outgoing_edges (i);
42
+ for (auto & e : edges)
43
+ os << " {" << e.dest << " : " << e.weight << " }, " ;
44
+
45
+ os << std::endl;
46
+ }
47
+
48
+ return os;
49
+ }
50
+
32
51
template <typename T>
33
52
class Graph
34
53
{
@@ -49,7 +68,7 @@ class Graph
49
68
return edge_list;
50
69
}
51
70
52
- void add_edge (Edge<T>& e)
71
+ void add_edge (Edge<T>&& e)
53
72
{
54
73
// Check if the source and destination vertices are within range
55
74
if (e.src >= 1 && e.src <= V &&
@@ -73,31 +92,13 @@ class Graph
73
92
74
93
// Overloads the << operator so a graph be written directly to a stream
75
94
// Can be used as std::cout << obj << std::endl;
76
- template <typename T>
77
- friend std::ostream& operator <<(std::ostream& os, const Graph<T>& G);
95
+ friend std::ostream& operator << <>(std::ostream& os, const Graph<T>& G);
78
96
79
97
private:
80
98
size_t V; // Stores number of vertices in graph
81
99
std::vector<Edge<T>> edge_list;
82
100
};
83
101
84
- template <typename T>
85
- std::ostream& operator <<(std::ostream& os, const Graph<T>& G)
86
- {
87
- for (auto i = 1 ; i < G.vertices (); i++)
88
- {
89
- os << i << " :\t " ;
90
-
91
- auto edges = G.outgoing_edges (i);
92
- for (auto & e : edges)
93
- os << " {" << e.dest << " : " << e.weight << " }, " ;
94
-
95
- os << std::endl;
96
- }
97
-
98
- return os;
99
- }
100
-
101
102
template <typename T>
102
103
auto create_reference_graph ()
103
104
{
@@ -169,4 +170,4 @@ int main()
169
170
test_BFS<T>();
170
171
171
172
return 0 ;
172
- }
173
+ }
0 commit comments