exception:list iterator not decremental

Hi.
In my code I used a list to simulate a circle and declared two iterators to traverse it.
while running,an exception appeared after eight elements was printed,stating that list iterator not decremental.
what caused this and how do i fix it?
here's my code:
#include<iostream>
#include<list>
using namespace std;
list<int> l;
int n,k,m;
int main(){
freopen("in.txt","r",stdin);
while(cin>>n>>k>>m&&n){
for(int i=1;i<=n;i++) l.push_back(i);
k%=n;
m%=n;
list<int>::iterator it1=l.begin();
list<int>::reverse_iterator it2=l.rbegin();

while(!l.empty()){
k%=l.size();
m%=l.size();
for(int j=0;j<k;j++){
it1++;
if(it1==l.end())
it1=l.begin();
}
for(int j=0;j<m;j++){
it2++;
if(it2==l.rend())
it2=l.rbegin();
}
if(*it1==*it2){
it2++;
if(it2==l.rend()) it2=l.rbegin();
l.erase(it1++);
if(it1==l.end()) it1=l.begin();
}
else{
printf("%d %d ,",*it1,*it2);
it1=l.erase(it1);
l.erase((it2++).base());
if(it1==l.end()) it1=l.begin();
if(it2==l.rend()) it2=l.rbegin();
}
}
printf("\n");
}
return 0;
}

Use code tags, don't cross post

http://cplusplus.com/forum/general/24602/
Last edited on
Topic archived. No new replies allowed.