简体   繁体   English

如何从文本文件读取并将数据添加到C ++中的图形

[英]How to read from a text file and add that data to a graph in c++

I have a text file that has 256 pairs of data. 我有一个包含256对数据的文本文件。 I need to take those pairs and put them into the vectors for the graph. 我需要将这些对放入图的向量中。 I am know how to do this in C# but I am new to C++. 我知道如何在C#中执行此操作,但是我是C ++的新手。 The format of the text file is 文本文件的格式为

125, 151
124, 176
ect...

I need it to come into the vectors for the graph in the format of graph[n][m], where n = 256 and m=256. 我需要它以graph [n] [m]的格式进入图的向量,其中n = 256和m = 256。 So I would read through the file and would mark 1 on the number that corresponds with the L/R Pair. 因此,我将通读该文件,并在与L / R对对应的数字上标记1。 So for example 125, 151. I would go to the 125th line and I would mark the 151'st 0 to be 1. 例如125、151。我将转到第125行,并将151st 0标记为1。

[n][m]{{0,0,0... 1(//176th 0),0,0,0...}, //124th line
{0,0,0... 1(//151st 0),0,0,0...}, //125th line
ect.

Does C++ have anything like the streamreader method out of C#? C ++是否具有C#之外的类似streamreader方法的东西?

Here is a sample of the vectors for a 7x7 max flow problem. 这是7x7最大流量问题的矢量样本。

int graph[V][V] = { {0, 6, 7, 0, 0, 0, 0},
                        {0, 0, 1, 3, 4, 0, 0},
                        {0, 0, 0, 2, 0, 5, 0},
                        {0, 0, 0, 0, 3, 2, 0},
                        {0, 0, 0, 0, 0, 0, 7},
                        {0, 0, 0, 0, 2, 0, 4},
                        {0, 0, 0, 0, 0, 0, 0}
                      };

as @Beta said in the comments under your question, you want to 就像@Beta在您的问题下的评论中所说,您要
1) create a 2-dimensional container full of zeroes 1)创建一个充满零的二维容器
2) read numbers from a text file 2)从文本文件中读取数字
3) change some of elements in the container according to the numbers. 3)根据编号更改容器中的某些元素。

So here some tips: 所以这里有一些提示:
1- For creating a 2D container: 1-创建2D容器:

 auto a = new int[100, 100]{0};

in this code you made an array of int s which is full of zeros. 在这段代码中,您制作了一个由0组成的int数组。 the elements that were not initialized in { } part, would set to default value. { }部分中未初始化的元素将设置为默认值。 which is zero for int. 对于int,该值为零。

2- reading numbers from a text file: 2-从文本文件中读取数字:

#include <iostream>
#include <fstream>

and in your code: 并在您的代码中:

int x , y;
ifstream fin("yourFile.txt");
fin >> x >> y; //Jusy like "cin"
//Do what you want
//and after that close the stream
fin.close();

3- change some of elements in the container according to the numbers: Simply do it like this: 3-根据数字更改容器中的某些元素:只需这样做:

a[i,i] = x;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM