Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext

Erasing and disposing values from Boost.Intrusive containers

從 Boost.Intrusive 容器中移除和處置值

One of the most tedious tasks when using intrusive containers is the management of the erased elements. When using STL containers, the container itself unlinks and destroys the contained elements, but with intrusive containers, the user must explicitly destroy the object after erasing an element from the container. This makes STL-like functions erasing multiple objects unhelpful: the user can't destroy every erased element. For example, let's take the function remove_if from list:
使 用介入式容器時最乏味的工作之一就是已刪除元素的管理的。使用 STL 容器時,容器本身負責刪除和銷毀所含的元素,但對於介入式容器,用戶必須在從容器中刪除對像後再明確地銷毀它。這使得類似於 STL 的刪除多個對象的函數沒用了:用戶不能銷毀每一個被刪除的元素。例如,我們看一下來自於 listremove_if

template<class Pred>
void remove_if(Pred pred);

How can the user destroy the elements (say, using operator delete) that will be erased according to the predicate? Boost.Intrusive containers offer additional functions that take a function object that will be called after the element has been erased from the container. For example, list offers:
用戶怎麼能夠銷毀(即使用 operator delete)依據謂詞被刪除的元素呢?Boost.Intrusive 容器提供了另一個函數,接受一個函數對象,該函數對像將在從容器中刪除某個元素後被調用。例如,list 提供了:

template<class Pred, class Disposer>
void remove_and_dispose_if(Pred pred, Disposer disposer)

With this function the user can efficiently remove and destroy elements if the disposer function destroys an object: remove_and_dispose_if will call the "disposer" function object for every removed element. list offers more functions taking a disposer function object as argument, like erase_and_dispose, clear_and_dispose, remove_and_dispose, etc.
如果 disposer 函數是銷毀一個對象的話,用戶就可以用這個函數高效地移除並銷毀元素:remove_and_dispose_if 將對每一個被移除的元素調用 "disposer" 函數對象。list 提供了多個接受一個 disposer 函數對像參數的函數,如 erase_and_dispose, clear_and_dispose, remove_and_dispose, 等等。

Note that the disposing function does not need to just destroy the object. It can implement any other operation like inserting the remove object in another container. Let's see a small example:
注意,處置函數並不必只是銷毀對象。它可以實現其它任何操作,如將移除的元素插入到另一個容器中。我們來看一個小例子:

#include <boost/intrusive/list.hpp>

using namespace boost::intrusive;

//A class that can be inserted in an intrusive list 一個可插入到介入式鏈表中的類
class my_class : public list_base_hook<> { public: my_class(int i) : int_(i) {} int int_; //...
}; //Definition of the intrusive list 介入式鏈表的定義
typedef list<my_class> my_class_list; //The predicate function 謂詞函數
struct is_even { bool operator()(const my_class &c) const { return 0 == (c.int_ % 2); } }; //The disposer object function 處置對象的函數
struct delete_disposer { void operator()(my_class *delete_this) { delete delete_this; } }; int main() { const int MaxElem = 100; //Fill all the nodes and insert them in the list 填充所有節點並將它們插入到鏈表中
my_class_list list; try{ //Insert new objects in the container 將新對像插入到容器
for(int i = 0; i < MaxElem; ++i) list.push_back(*new my_class(i)); //Now use remove_and_dispose_if to erase and delete the objects 現在使用 remove_and _dispose_if 來移除並銷毀對像
list.remove_and_dispose_if(is_even(), delete_disposer()); } catch(...){ //If something throws, make sure that all the memory is freed 如果有異常拋出,確保所有內存被釋放
list.clear_and_dispose(delete_disposer()); throw; } //Dispose remaining elements 處置剩餘的元素
list.erase_and_dispose(list.begin(), list.end(), delete_disposer()); return 0; }

All Boost.Intrusive containers offer these "erase + dispose" additional members for all functions that erase an element from the container.
所有 Boost.Intrusive 容器都為所有從容器中移除元素的函數提供了這些 "移除 + 處置" 的成員。


PrevUpHomeNext