I am trying to make multi-weighted graphs.
Input is to be made in following way
- Firstly user number of vertex user want and
- The ith line of a programmer’s map contains details of the vertex
directly accessible from room i . for ex
5
a 2 // (vertex 1 is connected to vertex 2 by edge having weight ‘a’)
t 5 r 4 // (vertex 2 is connected to vertex 4 an by edge having weight ‘t’and’r’)
a 4
r 2 t 3
b 5 i 5 o 5
#include <iostream>
#include <vector>
#include <sstream>
using namespace std;
struct maps{
vector<char> weight; //for storing multiple-edge and self-loop![enter image description here][1]
};
void input_edge(int n,maps m[10][10])
{
std::string user_input;
std::istringstream iss(user_input);
char letter;// for making tokens
int index; // for making tokens
int i;
for(i=1;i<=n;i++)
{ std::getline(std::cin, user_input);
while (iss >> letter >> index)
m[i][index].weight.push_back(letter);
}
}
int main()
{ int n;//no.of vertex user want to make graph
cin>>n;
cin.ignore(1000,'\n');
maps m1[10][10];
input_edge(n,m1);
int i,j;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{cout<<"no of edge between vertex"<<i<<' '<<"and"<<' '<<j<<':'<<m1[i][j].weight.size()<<endl;}//to find no of edge between 2 vertex
}
but i am getting incorrect outputs.please tell how to fix it..
You don’t quite understand the use of
istringstream. On this linestd::istringstream iss(user_input);you fill in all the content you will read from the input string stream. From then on nevermind how many things you read instd::getline(std::cin, user_input);you never change the contents ofiss, meaning that the input string stream will never serve you anything but nulls. Change your code like that:And see if there is any difference.