我有一个基本的工作,只产生一个随机的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