问题
I have a text file that has 256 pairs of data. 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++. 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. So I would read through the file and would mark 1 on the number that corresponds with the L/R Pair. So for example 125, 151. I would go to the 125th line and I would mark the 151'st 0 to be 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#?
Here is a sample of the vectors for a 7x7 max flow problem.
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}
};
回答1:
as @Beta said in the comments under your question, you want to
1) create a 2-dimensional container full of zeroes
2) read numbers from a text file
3) change some of elements in the container according to the numbers.
So here some tips:
1- For creating a 2D container:
auto a = new int[100, 100]{0};
in this code you made an array of int
s which is full of zeros. the elements that were not initialized in { }
part, would set to default value. which is zero for int.
2- reading numbers from a text file:
#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:
a[i,i] = x;
来源:https://stackoverflow.com/questions/32814392/how-to-read-from-a-text-file-and-add-that-data-to-a-graph-in-c