Reputation: 3
What is the reason for this error?Actually I dont want to get affected by the previous testcase result so at beginning of test case,I emptied the queue so that each test case could get fresh start. "
#include <iostream>
#include <queue>
using namespace std;
int main()
{
queue <int> first;
queue <int> empty;
queue <int> second;
int i,j,k,l,n,m,a,p,q;
int test,t;
std::cin>>test;
t=test;
while(test--)
{
swap(first,empty);
swap(second,empty);
std::cin>>n>>m;
a=2*n;
for(i=1;i<=a;i++)
{
first.push(i);
}
for(i=a+1;i<=m;i++)
{
second.push(i);
}
for(i=1;i<=a;i++)
{
p=second.front();
second.pop();
cout<<p;
q=first.front();
first.pop();
cout<<q;
}
}
}
Upvotes: 0
Views: 709
Reputation: 334
The problem here is in the lines,
swap(first, empty);
swap(second, empty);
When the first swap is called, 'empty' gets 'first' (and 'first' becomes 'empty'). Now, when the second swap is called, empty is no longer vacant (It has 'first').
To solve this problem, One solution could be,
first = queue<int> ();
second = queue<int> ();
to empty the queues.
Another solution could be to write,
first = empty;
second = empty;
Upvotes: 1