1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
#include "gdwg_graph.h"
#include <iostream>
#include <string>
auto main() -> int {
/*
auto g = gdwg::graph<std::string, int>{};
g.insert_node("hello");
g.insert_node("how");
g.insert_node("are");
g.insert_node("you?");
g.insert_edge("hello", "how", 5);
g.insert_edge("hello", "are", 8);
g.insert_edge("hello", "are", 2);
g.insert_edge("how", "you?", 1);
g.insert_edge("how", "hello", 4);
g.insert_edge("are", "you?", 3);
std::cout << g << "\n";
auto g2 = gdwg::graph<std::string, int>(g);
std::cout << g2 << "\n";
// This is a structured binding.
// https://en.cppreference.com/w/cpp/language/structured_binding
// It allows you to unpack your tuple.
for (const auto& [from, to, weight] : g) {
std::cout << from << " -> " << to << " ";
if (weight.has_value()) {
std::cout << "(weight " << *weight << ")\n";
}
else {
std::cout << "(no weight)\n";
}
}
*/
using graph = gdwg::graph<int, int>;
auto const v = std::vector<std::tuple<int, int, std::optional<int>>>{
{4, 1, -4},
{3, 2, 2},
{2, 4, std::nullopt},
{2, 4, 2},
{2, 1, 1},
{4, 1, std::nullopt},
{6, 2, 5},
{6, 3, 10},
{1, 5, -1},
{3, 6, -8},
{4, 5, 3},
{5, 2, std::nullopt},
};
auto g = graph{};
for (const auto& [from, to, weight] : v) {
g.insert_node(from);
g.insert_node(to);
if (weight.has_value()) {
g.insert_edge(from, to, weight.value());
}
else {
g.insert_edge(from, to);
}
}
g.insert_node(64);
auto out = std::ostringstream{};
out << g;
std::cout << g << '\n';
}
|