我有一個基本的工作,只產生一個隨機的10個狀態序列,0 =產生,1 =消耗。對於每0,我需要填充該列表,如果它完全沒有做任何事情,併爲1,清空列表...如果它已經是空的,什麼都不做。這個C++列表有什麼問題?
我做了簡單的pop_front,push_front把東西放入列表中......但我不知道爲什麼這是錯誤的......任何想法?
#include <iostream>
#include <list>
#include <cstdlib>
using namespace std;
int main(){
list<int> MyList;
int random;
for (int i=10; i>0; i--){
random = rand() % 2;
if(random == 0){
if(MyList.front() == NULL){
for(int k=10; k>0; k--){
MyList.push_front(k);
}
}
} else if(random == 1){
if(MyList.front() != NULL){
for(int j=10; j>0; j--){
MyList.pop_front();
}
}
}
std::cout << random << ", ";
}
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
return 0;
}
你知道它有什麼問題嗎?當你運行它時會發生什麼,以及它與你期望的有什麼不同? – 2012-01-31 05:21:58
是什麼讓你覺得這是錯的? – Borealid 2012-01-31 05:22:28