如何接受来自输入文件的图形
How to accept graph from input file
>我接受来自输入文件的无向加权图。文件包含200
节点。
我写了这样的代码。
typedef pair<int, int> ipair;
class Graph
{
int V;
list< pair <int, int> > *adj;
public:
Graph(int V);
};
Graph::Graph( int V)
{
this-> V = V;
adj = new list<ipair>[V];
}
bool Graph :: read_file()
{
const long int N = 1000000;
std::ifstream infile("Dijkstra.txt");
if(!infile.is_open()) return false;
std::string line;
int i = 0;
while ( i < N && getline(infile, line) )
{
std::istringstream str(line);
int u;
str >> u;
if ( u > N )
{
// Problem.
abort();
}
int v;
int w;
while ( str >> v >> w)
{
adj[u].push_back(make_pair(v,w));
}
++i;
}
}
int main()
{
Graph g(200);
g.read_file();
g.print_graph();
return 0;
}
I/P file :
1 80,982 163,8164 170,2620 145,648 200,8021 173,2069 92,647 26,4122 140,546 11,1913 160,6461 27,7905 40,9047 150,2183 61,9146 159,7420 198,1724 114,508 104,6647 30,4612 99,2367 138,7896 169,8700 49,2437 125,2909 117,2597 55,6399
2 42,1689 127,9365 5,8026 170,9342 131,7005 172,1438 34,315 30,2455 26,2328 6,8847 11,1873 17,5409 157,8643 159,1397 142,7731 182,7908 93,8177
节点 1 连接到权重为 982
的节点 80节点 1 连接到权重为 8164 的节点 163
节点 2 连接到权重为 1689
的节点 42节点 2 连接到权重为 9365
的节点 127等。。。。。
现在有了这个,我可以接受节点 1 连接到权重为 80 (1 80,982( 的节点 1 但是与节点 1 连接的其余节点呢?
即如何制作接受 v 和 w 的循环?
我认为您可以使用
vector<list< pair <int, int> >> adj
来存储数据,而不是在 Graph 类中list< pair <int, int> > *adj
。
我稍微修改了您的程序以存储与节点相关的所有数据对。
#include <iostream>
#include <utility>
#include <list>
#include <fstream>
#include <sstream>
#include <cstdlib>
#include <vector>
using namespace std;
typedef pair<int, int> ipair;
class Graph
{
int V;
vector<list< pair <int, int> >> adj;
public:
bool read_file();
void print_graph();
Graph(int V);
};
Graph::Graph( int V)
{
this-> V = V;
adj.reserve(V);
}
bool Graph:: read_file()
{
const long int N = 1000000;
std::ifstream infile("Dijkstra.txt");
if(!infile.is_open()) return false;
std::string line;
int i = 0;
while ( i < N && getline(infile, line) ) {
std::istringstream str(line);
int u;
str >> u;
if ( u > N ) {
// Problem.
abort();
}
int v;
int w;
char c;
while ( str >> v >> c >> w) {
if (u <= (int)adj.size()) { // index (u-1) exists
auto list = adj[u-1]; // get the existing list
list.push_back(make_pair(v,w)); // add new data
adj[u-1] = list; // store it the same index
} else { // index (u-1) doesn't exist
list<ipair> list; // create a new list
list.push_back(make_pair(v,w)); // store the values
adj.push_back(list); // add it in the vector
}
}
++i;
}
return true;
}
void Graph::print_graph() {
int node = 1;
for (auto& x : adj) {
cout << "from node: " << node++ << endl;
for (auto it = begin(x); it != end(x); ++it) {
cout << it->first << " " << it->second << "n";
}
}
}
int main() {
Graph g(200);
g.read_file();
g.print_graph();
return 0;
}
你可以正则表达式匹配,。
例如
std::regex pairs_regex("(d+),(d+)");
auto pairs_it = std::sregex_iterator(line.begin(), line.end(), pairs_regex);
auto pairs_end = std::sregex_iterator();
for(;pairs_it != pairs_end; ++pairs_it)
{
std::string v = pairs_it->str(1);
std::string w = pairs_it->str(2);
adj[u].push_back(make_pair(std::stoi(v), std::stoi(w)));
}
注意:如果adj
是一个std::
容器,而不是一个原始数组,会更安全。环体将填充一个list<pair<int, int>> temp
,然后adj.emplace_back(std::move(temp))
相关文章:
- 使用 scanf 读取文件中的图形时出现问题
- SFML - 代码::块看不到 sfml 图形(等)文件
- 如何接受来自输入文件的图形
- c/c 的文件/符号依赖图形可视化
- 在Visual Studio Community 2019中生成包含文件的图形
- 在 C++ 中从文本文件快速创建图形
- 从输入文件打印图形
- C++图形初始化错误(语法或丢失文件缺陷?
- 将图形插入C 中的HTML文件
- 基于文件的输入来定义图形变量
- 如何使用 boost 从文件中读取图形
- 读取图形可视化的点文件,而无需在提升图中存储节点 ID
- 用于将三维模型导入OpenGL/C++项目的推荐文件格式和图形库
- 如何在文件中存储图形
- 我正在尝试通过 Boost 图形库从 Graphviz DOT 文件中读取图形.如何读取存储在数组中的未知数量的属性
- OpenGL代码显示人体图形,从二进制文件中读取顶点
- 无法使用 GCC 打开图形文件 GCOV
- 来自文件 c++ 的图形
- C/C++:如何使用制表从文本文件中读取序列化图形(树)
- 是否可以在pdf文件中重新初始化图形状态