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...
CodePudding user response:
I am sorry for how this question was stated. I agree it wasn't well paraphrased. Anyways... I managed to do the job by adding this code:
void readInstance(string filename, Instance &instance){
instance.instanceName = filename;
ifstream file(filename);
if (file.is_open()) {
string line;
int i = 0;
while (getline(file, line)) {
if(i == 0){
instance.n = stoi(line);
}
else{
istringstream ss(line);
string aux1, aux2, aux3;
ss >> aux1;
ss >> aux2;
ss >> aux3;
Edge p;
p.source = stod(aux1);
p.dest = stod(aux2);
p.weight = stod(aux3);
instance.Edges.push_back(p);
}
i ;
}
file.close();
}
else {
cout << "Error opening instance file" << endl;
exit(1);
}
}
CodePudding user response:
Assuming that you have input in the following format in the text file
0 1 2
0 2 3
0 3 3
1 2 4
You can use the following code to read integers directly into the vector. No need to use std::transform
to convert string into integer.
#include <iostream>
#include <vector>
#include <fstream>
struct Edge {
int source, dest, weight;
};
std::ostream& operator<<(std::ostream& os, const Edge edge) {
os << "(" << edge.source << ", " << edge.dest << ", " << edge.weight << ")\n";
return os;
}
int main()
{
// initialize edges as per the above diagram
// (u, v, w) represent edge from vertex `u` to vertex `v` having weight `w`
std::vector<Edge> edges;
std::ifstream fin("in.txt");
if (!fin.is_open()) {
std::cerr << "fail";
return 1;
}
int s, d, w;
while (fin >> s >> d >> w) {
edges.emplace_back(Edge{s, d, w});
}
fin.close();
for (auto &edge: edges) {
std::cout << edge;
}
return 0;
}