list iterator not decrementable
江尹城 2015-10-25 11:14:17 再用STL List写Josephus环问题的时候遇到了list iterator not decrementable,网上看了一些解答也完成不了,求大神来解答一下,感激不尽!!
#include<iostream>
#include<list>
using std::list;
using namespace std;
int InitJCircle(int n,int k){
list<int> ilist;
for (int i = n; i >= 1; i--){
ilist.push_front(i);
}//Init
int order[100];
list<int>::iterator it = --( ilist.begin());
while (!ilist.empty()){
int i = 0;
for (int j = 1; j <= k; j++){
if (++it == ilist.end())
it=ilist.begin(); }
order[i] = *it;
ilist.erase(it);
i++;
}//GetOrder
int i = 0;
while (order[i]){
cout << order[i] << " ";
i++;
}
return 0;
}
int main(){
int n, k;
cout << "Please input the n and k:" << endl << "n = ";
cin >> n;
cout << endl << "k = ";
cin >> k;
cout << "The order is :";
InitJCircle(n,k);
}