C++ 使用字符串流c++;
我需要解析此表单的图形文件:C++ 使用字符串流c++;,c++,parsing,stringstream,C++,Parsing,Stringstream,我需要解析此表单的图形文件: 5 0 1 0.2 3 10.1 4 0.5 -1 1 0 1.5 -1 2 1 100.0 3 50.2 -1 3 -1 4 1 10.5 2 13.9 -1 其中第一行是节点数。从二线开始, 0 1 0.2 3 10.1 4 0.5 -1 0是源节点,1是它移动到的节点,0.5是边的权重-1表示行尾 我创建了一个graph类: #ifndef GRAPH_H #define GRAPH_H #include <vector> #include
5
0 1 0.2 3 10.1 4 0.5 -1
1 0 1.5 -1
2 1 100.0 3 50.2 -1
3 -1
4 1 10.5 2 13.9 -1
其中第一行是节点数。从二线开始,
0 1 0.2 3 10.1 4 0.5 -1
0是源节点,1是它移动到的节点,0.5是边的权重-1表示行尾
我创建了一个graph类:
#ifndef GRAPH_H
#define GRAPH_H
#include <vector>
#include <iostream>
class Graph{
public:
explicit Graph(int size=0) : vertices_(size) { };
void resize(int size){
vertices_.resize(size);
empty();
}
void insert(int v, int n, double w){
Vertex* tmp = new Vertex{ n, w, nullptr }
cout << " inserting!";
end(v)->next = tmp;
}
void empty(){
for(int i=0;i<vertices_.size();i++)
vertices_[i] = new Vertex{i,0,nullptr}
}
void print() {
for(auto& v : vertices_){
Vertex* tmp = v;
cout<< " Node " << v->node << " has edges to: \n"
while(tmp->next != nullptr){
cout<< " node " << tmp->node << " with weight " << tmp->weight<<endl;
}
cout<<endl;
}
}
private:
struct Vertex{ // struct for vertices of graph
int node;
double weight;
Vertex* next;
Vertex (int n, double w, Vertex* v)
: node{ n }, weight{ w }, next{ v } { }
};
vector<Vertex*> vertices_;
Vertex* end(int v){
Vertex* tmp = vertices_[v];
while(tmp->next != nullptr)
tmp = tmp->next;
return tmp;
}
};
#endif
#ifndef图
#定义图
#包括
#包括
类图{
公众:
显式图(int size=0):顶点(size){};
空心调整大小(整数大小){
顶点。调整大小(大小);
空();
}
空白插入(整数v、整数n、双w){
顶点*tmp=新顶点{n,w,nullptr}
cout-next=tmp;
}
void empty(){
对于(inti=0;i您应该忽略行,只读取整数和双精度
int n; f >> n;
for (int i=0; i<n; i++) {
int v; f >> v;
assert(v == i);
int other; f >> other;
while (other != -1) {
double weight; f >> weight;
add_edge(v, other, weight);
f >> other;
}
}
intn;f>>n;
对于(int i=0;i>v;
断言(v==i);
int-other;f>>其他;
而(其他!=-1){
双倍重量;f>>重量;
添加_边缘(v、其他、重量);
f>>其他;
}
}
这假设根本没有错误检查您忘记提前处理temp变量
#include <bits/stdc++.h>
using namespace std;
class Graph {
public:
explicit Graph(int size=0) : vertices_(size) { };
void resize(int size){
vertices_.resize(size);
empty();
}
void insert(int v, int n, double w){
end(v)->next = new Vertex{ n, w, nullptr };
cout << " inserted! " << endl;
}
void empty(){
for(int i = 0; i < vertices_.size(); i++)
vertices_[i] = new Vertex{ i, 0, nullptr};
}
void print() {
for(auto& v : vertices_){
Vertex* tmp = v;
cout<< " Node " << v->node << " has edges to: \n";
while(tmp->next != nullptr) {
if(tmp->node != v->node)
cout<< " Node " << tmp->node << " with weight " << tmp->weight<<endl;
tmp = tmp->next; // You forgot this line
}
cout<<endl;
}
}
private:
struct Vertex{ // struct for vertices of graph
int node;
double weight;
Vertex* next;
Vertex (int n, double w, Vertex* v)
: node{ n }, weight{ w }, next{ v } { }
};
vector<Vertex*> vertices_;
Vertex* end(int v){
Vertex* tmp = vertices_[v];
while(tmp->next != nullptr)
tmp = tmp->next;
return tmp;
}
}g;
int main()
{
int N,x,y;
double w;
cin >> N;
g.resize(N);
for(int i = 0; i < N; i++)
{
cin >> x;
while(cin >> y, y != -1)
{
cin >> w;
cout << "Edge from " << x << " to " << y << " with cost " << w << endl;
g.insert(x,y,w);
}
}
g.print();
}
#包括
使用名称空间std;
类图{
公众:
显式图(int size=0):顶点(size){};
空心调整大小(整数大小){
顶点。调整大小(大小);
空();
}
空白插入(整数v、整数n、双w){
end(v)->next=新顶点{n,w,nullptr};
cout x;
而(cin>>y,y!=-1)
{
cin>>w;
cout>w;
你能给我们看一下你的解析函数的其余部分吗?`bool first=true;stringstream parse;int n,s;double w;string line;ifstream graph1(argv[1]);Graph g;`你能用那个代码编辑你的问题吗?我发现代码很混乱…它混合了C风格的列表和STL容器。我会用一个std::vector而不是std::vector,然后需要一个edge对象,可能只是std::pair的形式。每个顶点都可以得到一个std::vector我如何从文件中获取数字?
#include <bits/stdc++.h>
using namespace std;
class Graph {
public:
explicit Graph(int size=0) : vertices_(size) { };
void resize(int size){
vertices_.resize(size);
empty();
}
void insert(int v, int n, double w){
end(v)->next = new Vertex{ n, w, nullptr };
cout << " inserted! " << endl;
}
void empty(){
for(int i = 0; i < vertices_.size(); i++)
vertices_[i] = new Vertex{ i, 0, nullptr};
}
void print() {
for(auto& v : vertices_){
Vertex* tmp = v;
cout<< " Node " << v->node << " has edges to: \n";
while(tmp->next != nullptr) {
if(tmp->node != v->node)
cout<< " Node " << tmp->node << " with weight " << tmp->weight<<endl;
tmp = tmp->next; // You forgot this line
}
cout<<endl;
}
}
private:
struct Vertex{ // struct for vertices of graph
int node;
double weight;
Vertex* next;
Vertex (int n, double w, Vertex* v)
: node{ n }, weight{ w }, next{ v } { }
};
vector<Vertex*> vertices_;
Vertex* end(int v){
Vertex* tmp = vertices_[v];
while(tmp->next != nullptr)
tmp = tmp->next;
return tmp;
}
}g;
int main()
{
int N,x,y;
double w;
cin >> N;
g.resize(N);
for(int i = 0; i < N; i++)
{
cin >> x;
while(cin >> y, y != -1)
{
cin >> w;
cout << "Edge from " << x << " to " << y << " with cost " << w << endl;
g.insert(x,y,w);
}
}
g.print();
}
int main()
{
ifstream myfile("in.txt");
if(myfile.is_open())
{
int N,x,y;
double w;
myfile >> N;
for(int i = 0; i < N; i++)
{
myfile >> x;
while(myfile >> y, y != -1)
{
myfile >> w;
cout << "Edge from " << x << " to " << y << " with cost " << w << endl;
}
}
myfile.close();
}
else
cout << "Unable to open file";
return 0;
}