I'm working with a custom vector, and currently the code looks like this:
struct Edge {
int source, dest, weight;
};
int main()
{
// initialize edges as per the above diagram
// (u, v, w) represent edge from vertex `u` to vertex `v` having weight `w`
vector<Edge> edges =
{
{0, 1, 10}, {0, 4, 3}, {1, 2, 2}, {1, 4, 4}, {2, 3, 9},
{3, 2, 7}, {4, 1, 1}, {4, 2, 8}, {4, 3, 2}
};
// total number of nodes in the graph (labelled from 0 to 4)
int n = 5;
// construct graph
Graph graph(edges, n);
}
I want to change from using hard-coded values into using a .txt
file that will look like this:
0 1 2
0 2 3
0 3 3
1 2 4
How can I switch into taking those numbers in the same fashion as before, but with a .txt
input instead of hard-coded numbers?
I've tried things like this:
std::vector<std::string> vecOfStr;
bool result = getFileContent("my/path/to/file", vecOfStr);
std::vector<int> ints;
std::transform(vecOfStr.begin(), vecOfStr.end(), std::back_inserter(edges),
[&](std::string s) {
std::stringstream ss(s);
int i;
ss >> i;
return i;
});
for (auto &i: edges) {
//std::cout << i << ' ';
}
But didn't succeed.
I have a problem because reading from a file is always as strings and I somehow need to transform each line to my custom struct.
Offtopic: BTW, it's a Dijkstra algorithm path finding program, finding path for each vertice...