For the input,
1
4 2
1 2
1 3
1
The Program,
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int Q;
cin >> Q; //Q=2
while(Q--)
{
int N, E;
cin >> N >> E; // N=4, E=2
while(1)
{
string edge;
getline(cin,edge); //edge should store "1 2" for the first iteration and "1 3" for the second iteration.
cout << edge << " " << edge.size() << endl;
vector<int> adjList[N];
if(edge.size()>1)
{
int u = stoi(edge.substr(0,1));
int v = stoi(edge.substr(2,1));
for(int i=0; i<E; ++i)
{
adjList[u].push_back(v);
adjList[v].push_back(u);
}
}
else
break;
}
}
return 0;
}
gives output
0
The program should print out,
1 2 3
1 3 3
1 1
Why am I not storing any value in the string edge? I think it's the getline()
function which causes the problem. Are there other functions that can take the whole line as into a string (including spaces) until a newline is reached?