Примерпользовательский интервалпоказано, как использовать интервальные контейнеры с собственным типом интервального класса.
#include<iostream>#include<boost/icl/interval_set.hpp>usingnamespacestd;usingnamespaceboost::icl;// Here is a typical class that may model intervals in your application.classMyInterval{public:MyInterval():_first(),_past(){}MyInterval(intlo,intup):_first(lo),_past(up){}intfirst()const{return_first;}intpast()const{return_past;}private:int_first,_past;};namespaceboost{namespaceicl{// Class template interval_traits serves as adapter to register and customize your interval classtemplate<>structinterval_traits<MyInterval>//1. Partially specialize interval_traits for {// your class MyInterval//2. Define associated typestypedefMyIntervalinterval_type;//2.1 MyInterval will be the interval_typetypedefintdomain_type;//2.2 The elements of the domain are ints typedefstd::less<int>domain_compare;//2.3 This is the way our element shall be ordered.//3. Next we define the essential functions // of the specialisation//3.1 Construction of intervalsstaticinterval_typeconstruct(constdomain_type&lo,constdomain_type&up){returninterval_type(lo,up);}//3.2 Selection of values staticdomain_typelower(constinterval_type&inter_val){returninter_val.first();};staticdomain_typeupper(constinterval_type&inter_val){returninter_val.past();};};template<>structinterval_bound_type<MyInterval>//4. Finally we define the interval borders.{// Choose between static_open (lo..up)typedefinterval_bound_typetype;// static_left_open (lo..up]BOOST_STATIC_CONSTANT(bound_type,value=interval_bounds::static_right_open);};// and static_closed [lo..up] }}// namespace boost iclvoidcustom_interval(){// Now we can use class MyInterval with interval containers:typedefinterval_set<int,std::less,MyInterval>MyIntervalSet;MyIntervalSetmySet;mySet+=MyInterval(1,9);cout<<mySet<<endl;mySet.subtract(3)-=6;cout<<mySet<<" subtracted 3 and 6\n";mySet^=MyInterval(2,8);cout<<mySet<<" flipped between 2 and 7\n";}intmain(){cout<<">>Interval Container Library: Sample custom_interval.cpp <<\n";cout<<"-----------------------------------------------------------\n";cout<<"This program uses a user defined interval class:\n";custom_interval();return0;}// Program output:/*-----------------------------------------------------------------------------
>>Interval Container Library: Sample custom_interval.cpp <<
-----------------------------------------------------------
This program uses a user defined interval class:
{[1, 9)}
{[1, 3) [4, 6) [7, 9)} subtracted 3 and 6
{[1,2) [3,4) [6,7) [8,9)} flipped between 2 and 7
-----------------------------------------------------------------------------*/
Статья Custom interval раздела Chapter 1. Boost.Icl Examples может быть полезна для разработчиков на c++ и boost.
Материалы статей собраны из открытых источников, владелец сайта не претендует на авторство. Там где авторство установить не удалось, материал подаётся без имени автора. В случае если Вы считаете, что Ваши права нарушены, пожалуйста, свяжитесь с владельцем сайта.