lundi 1 juin 2020

How to read space, comma and newline separated integers from a file into an array of vectors in C++?

I have a file format like this:

1 80,982 163,8164 170,2620..
2 42,1689 163,1689 5,8026..
....

I also share the image of the file format: Image of the file format

where the first number represent the starting vertex of a graph.The second number separated by a space represent the destination vertex and the third number separated by a comma represent the weight of the edge connected by that two vertices.

For example- In the first row the number 1 means the starting vertex, 80 is the destination vertex and the weight of the edge is 982.

Here my code:

void addedge(vector <pair<int,int> > adj[],int start,int stop,int weight)
  {
   adj[start].push_back(make_pair(stop,weight));
   adj[stop].push_back(make_pair(start,weight));
  }

void print(vector <pair<int,int> > adj[],int num)
{   for(int i=0;i<num;i++)
    {
    cout<<i<<"\t";
    for(vector<pair<int,int> >::const_iterator iter=adj[i].begin();iter!=adj[i].end();iter++)
    {
        cout<<iter->first<<"\t";
        cout<<iter->second<<"\t";
    }
    cout<<endl; 
}


  int main()
  {
   int start,stop,weight,source,count=0;
  //    cout<<"Enter the number of vertices: ";
  //    cin>>num;
        vector <pair<int,int>> adj[200];
  //    cout<<"\nEnter (-1 -1 -1) for exit: ";
  /*    while(1)
        {
         cout<<"\nEnter the vertices and weight: ";
         cin>>start>>stop>>weight;
         if(start==-1 && stop==-1 && weight==-1)
            break;
          addedge(adj,start,stop,weight);
         }
  */}

Here I put the code of user interface.But I do not understand how to read the integers from the file and make the graph.

I am new in C++.I tried a lot but I could not figure it out.

Aucun commentaire:

Enregistrer un commentaire