I로 정의 목록을 갖도록 std::list<BunnyInfo> bList;
, 개인 BunnyInfo
목록이 멤버 함수를 통해 성장하는 구조해방 표준 : 목록 부재
struct BunnyList::BunnyInfo {
std::string name;
char gender;
std::string color;
unsigned int age : 6; // 0 - 63
bool mutant;
};
에게 인 클래스 내에
void BunnyList::add(int count){
bListIter iter;
while(count--){
BunnyInfo *bNew = &fill(*new BunnyInfo());
for(iter = bList.begin(); iter != bList.end(); iter++){
if(iter->age <= bNew->age)
break;
}
bList.insert(iter, *bNew);
}
}
곳 fill()
은 구조체의 값을 생성하는 함수입니다. 나는 또한 목록 구성원을 삭제하고 동시에 add()
를 통해 할당 된 자원을 해제 할 방법
void BunnyList::reap(){
int toKill = bList.size()/2;
int find;
bListIter iter;
while(toKill--){
find = rng(0, bList.size()-1);
iter = bList.begin();
for(int i = 0; i < find; i++) // traverse list to the find-th node;
iter++;
delete &(*iter);
bList.erase(iter);
}
}
내 질문은 절반 목록을 삭제하는 멤버 함수를 가지고있다. delete &(*iter);
은 오류가 발생하지만 프로그램이 없으면 프로그램이 정상적으로 실행됩니다. 그러나 erase()
을 호출해도 목록 노드와 연결된 BunnyInfo
이 해제되지는 않습니다.
STL을 처음 사용했습니다.
매우 특별한 이유가 없다면'std :: list'를 피하고 싶을 것입니다. ('std :: vector'는 거의 모든면에서 우수합니다.) –
add 함수가 동적으로 BunnyInfo를 할당하는 이유는 무엇입니까? –
글쎄, 목록은 '연령'에 따라 정렬됩니다. 중간 삽입을 많이하므로 벡터가 덜 이상적이라고 생각합니다. (?) – lightburst