簡體   English   中英

使用指針擦除std :: list中的元素?

[英]Erase element from std::list using a pointer?

class SororityBeerExpo{
public:

     std::list<LaysCrankdPepChip> m_chipList;

     void destroyChip(LaysCrankdPepChip * target)
     {
         // m_chipList needs to erase that which is pointed at by target but
         // erase() requires an iterator which is not what we got to work with
         // remove() needs a value which is not right either
     }
}

我的問題是,如何使用指向該元素的指針刪除列表中的元素? 我想這樣做而不使用迭代器代替指針。

您可以對列表中的元素進行[線性]搜索,將每個元素的地址與指針進行比較( std::find_if將符合條件)。 當然,最后你仍然會使用迭代器,因為這就是list::erase需要的東西。

你不能直接這樣做(雖然看到本傑明的答案是間接的做法)。 列表節點包含的數據多於僅包含的對象(例如指向前一個節點和下一個節點的指針),但您的原始指針僅指向包含的對象。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM