C++ boost::intrusive::list














































C++ boost::intrusive::list



INTRODUCTION:
C++ boost::intrusive::list is used to prepare a list of objects of any user-defined datatype. This list is knows as an
intrusive list. In a list, an element is always accessed from another element, usually using a pointer. If an intrusive list is to store objects of any datatype without dynamic memory allocation, pointers must exist somewhere to concatenate elements. To inherit objects, boost::intrusive provides hooks. Hooks are template classes that has default parameters. Intrusive containers do not store copies, you must remove objects from intrusive containers before you destroy them.

There are two functions available which act as linker from objects to list :
1. push_back() : To add items into the list
2. pop_back() : To delete items from the list

HEADER FILE: 
boost/intrusive/list.hpp

SAMPLE PROGRAM:
#include <boost/intrusive/list.hpp>
#include <string>
#include <utility>
#include <iostream>

using namespace boost::intrusive;
using namespace std;

struct animal : public list_base_hook<>
{
string name;
int legs;
animal(
string n, int l) : name{move(n)}, legs{l} {}
};

int main()
{
animal a1{
"aa", 1};
animal a2{
"bb", 2};
animal a3{
"cc", 3};

typedef list<animal> animal_list;
animal_list animals;

animals.push_back(a1);
animals.push_back(a2);
animals.push_back(a3);

for (const animal &a : animals)
cout << a.name << endl;
return 0;
}


OUTPUT:

In the above example, an intrusive list is created with objects aa,bb and cc.


Comments