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
|
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
class Edge
{
char from, to;
int value;
public:
friend std::istream& operator>>(std::istream& is, Edge& edge);
friend std::ostream& operator<<(std::ostream& os, const Edge& edge);
};
std::ostream& operator<<(std::ostream& os, const Edge& edge)
{
return os << '(' << edge.from << ',' << edge.to << ")=" << edge.value;
}
std::istream& operator>>(std::istream& is, Edge& edge)
{
char ch;
if (is >> ch && ch == '(' && is >> edge.from &&
is >> ch && ch == ',' && is >> edge.to &&
is >> ch && ch == ')' && is >> ch && ch == '=')
return is >> edge.value;
is.clear(is.failbit);
return is;
}
int main()
{
//std::ifstream in("your_file");
// for testing:
std::istringstream in(
"(A,B)=1\n"
"(A,F)=10\n"
"(F,C)=15\n"
"(E,D)=4\n"
"(B,E)=7\n"
"(F,B)=2\n");
std::vector<Edge> edges;
for (std::string line; std::getline(in, line); )
{
std::istringstream iss(line);
Edge edge;
if (!(iss >> edge))
break;
edges.push_back(edge);
}
for (const auto& e: edges)
std::cout << e << '\n';
}
|