Appearance
learn C++ static, namespace, comments, long snake_case naming method, file stream and string stream, header usage.
learn C++ full good practice by doing such project like shortest path problem.
header guard:
cpp
#ifndef DIJKSTRA_H
#define DIJKSTRA_Hensure compile once, equal to:
cpp
#pragma oncestructured bindings, const int &, auto
utilize const auto &[v, weight]:
cpp
for(const auto &[v,weight] : graph.get_neighbors(u)){
file << "a " << (u + 1) << " " << (v + 1) << " " << weight << "\n";
}random number generation
cpp
static Graph create_random_graph(int num_vertices, int num_edges, int max_weight = 1000) {
Graph graph(num_vertices);
random_device rd;
mt19937 gen(rd());
uniform_int_distribution<> vertex_dist(0, num_vertices - 1);
uniform_int_distribution<> weight_dist(1, max_weight);
for (int i = 0; i < num_edges; i++) {
int u = vertex_dist(gen);
int v = vertex_dist(gen);
if (u != v) {
int weight = weight_dist(gen);
graph.add_edge(u, v, weight);
}
}
return graph;
}txt
vector has
back(), pop_back(), begin(), end(), [], size()