I know the reason why this is happening but don't know how to solve this as I am new to STL.
I am taking the inputs from the user and representing the weighted graph using vectors. I declared a vector pair<int,int> to store the value of the edge and the weight.
#include<iostream>
#include<vector>
using namespace std;
int main()
{
int n,m,c;
cout<<"Enter n,m : "<<endl;
cin>>n>>m;
vector<pair<int,int>> adj[n+1];
cout<<"If un-directed input 1 else input 0: "<<endl;
cin>>c;
cout<<"Enter the values of u,v and the waight : "<<endl;
for(int i=0;i<m;i++)
{
int u,v,w;
cin>>u>>v>>w;
adj[u].push_back({v,w});
if(c==1)
adj[v].push_back({u,w});
}
for(int i=0;i<=n;i++)
{
for(int j:adj[i])
{
cout<<i<<"->" << j <<endl;
}
cout<<endl;
}
cout<<"Inserted Successfully";
return 0;
}
But when i am printing the values i am not able to compile the program. It is due to this for loop.
for(int j:adj[i])
{
cout<<i<<"->" << j <<endl;
}
As the value in adj[i] is a pair of edge and weight thus ,the for_each loop is unable to convert the pair value to a single int value. This might be the reason but i am unable to solve this as i am new to the STL.
Please Help.