【发布时间】:2014-08-05 08:25:47
【问题描述】:
我正在尝试在 C++ 中读取一个文件并填充我的向量,该向量表示一个邻接列表。该文件包含一个无向加权图的邻接列表表示。每一行由与该特定顶点相邻的节点元组组成与那条边的长度。例如,第 6 行的第一个条目为 6,表示该行对应于标记为 6 的顶点。该行的下一个条目“141,8200”表示在顶点 6 和顶点 141 之间存在一条长度为 8200 的边. 该行其余的对表示与顶点6相邻的其他顶点以及对应边的长度。
文件例如:-
1 3,4 2,20 5,89
2 4,7 1,102
ifstream ifs;
string line;
ifs.open("dijkstraData.txt");
cout<<log(vertices)<<" "<<loops<<endl;
std::vector<vector < std::pair < int,int > > > CadjList(vertices);
while(getline(ifs,line)){
// transfer the line contents to line_stream
stringstream line_stream(line);
//now what
}
【问题讨论】:
-
无法为您完成作业,但我可以提供提示:1) 拆分您的字符串 (stackoverflow.com/a/237280/1938163) 2) 再次拆分 3) 使用 cplusplus.com/reference/string/stoi 将字符串转换为整数 4) 填充您的数据向量。祝你好运。
-
@MarcoA。感谢您没有直接回答我。
标签: c++ algorithm graph stl adjacency-list