1
如何在C++ 矢量刪除節點刪除節點此代碼甚至不編譯如何從一個向量在C++
typedef vector<simple_node> container;
//======================================
// remove an item from the queue that matches what we arelady have
container parser::removeFromQueue(container local_container, simple_node *node_to_remove)
{
for (auto i = local_container.begin(); i != local_container.end(); i++)
{
if ((i->toy == node_to_remove->toy) &&
(i->type == node_to_remove->type))
{
local_container.erase(remove(local_container.begin(), local_container.end(), i), local_container.end());
break;
}
}
return local_container;
}
它未能就行了
local_container.erase(remove(local_container.begin(), local_container.end(), i), local_container.end());
編譯器抱怨查詢分配。
複製整個容器到函數和背出再次將是相當低效的相比於使用通按引用(即返回'void'和接受'容器&local_container'作爲參數 –