i have a graph file as
3200
12 16
114 61
37 465
.
.
.
and it goes like this.
the first number is vertexNumber other integers represents vertices and we have an edge between them. for example myMatrix[12][16] = 1 and myMatrix[16][12] = 1
i used ifstream to read the graph.txt (ifstream theGraphFile("graph.txt", ios::in);) and created a bool matrix with a constant size like 10000
my pattern for my purpose is this:
while( getline( theGraphFile, line ) )
{
if (line[1])
continue;
else
{
//parse
//matrix
}
}
so my question: how can i seperate those numbers as “before the space and until the end of the line” format. if my string in nth line is 12 51 i want to use them as matrix indexes like myMatrix[12][51] = 1
Thank you.. 🙂
2 ways come to mind: you can use the formatted extraction
operator >>from ifstream directly or use getline to put the entire line into a string first and then use istringstream to extract it into indices.If your graph file will only contain indices one after another, like your example, the first method will probably be easier:
The second method with getline will allow for some flexibility if each line has other info too: