我是來自boost的shared_ptr的新手,並且正在考慮迭代我的集合以獲取最佳對象。 編輯:關於first_world添加信息循環迭代後共享指針斷言失敗
std::set<World::CPtr> first_world = ... // long call, but it gets a set of constant shared pointers to the class World, where various methods exist
typedef boost::shared_ptr<World const> CPtr;
World::CPtr best = *(first_world.begin());
for (World::CPtr lo : first_world) {
if (best->getValue() >= lo->getValue()){
best = lo;
}
}
後來我想使用共享指針,我的程序與傳達斷言`PX崩潰= 0' 失敗!我遵循here的規則,我的意思是我在循環中使用了一個共享指針作爲迭代器,但隨後我將它分配給另一個指針。那是不好的做法,有沒有更好的做法?
cout << "name is: " << best->getDefinition() << endl;
爲什麼在明顯使用C++ 11時使用'boost :: shared_ptr'? – molbdnilo
@molbdnilo爲什麼不呢?似乎相當無關緊要。上次我檢查的類型是不一樣的,並且使用你的代碼庫需要的類型是有意義的... – sehe
well C++ 11沒有必要,我會將其更改爲舊標準 - 我想了解的是爲什麼這不起作用,如何使其工作。 – beginh