gpt4 book ai didi

c++ - boost interval_map 与 split_interval_map

转载 作者:行者123 更新时间:2023-11-30 03:50:01 25 4
gpt4 key购买 nike

我很难理解 interval_map 和 split_interval_map,我都实现了,结果是一样的。下面是分割区间图的代码。

#include <iostream>
#include <boost/icl/split_interval_map.hpp>
#include <boost/icl/interval_map.hpp>

using namespace std;
using namespace boost::icl;


/* The most simple example of an interval_map is an overlap counter.
If intervals are added that are associated with the value 1,
all overlaps of added intervals are counted as a result in the
associated values.
*/
typedef split_interval_map<int, int> OverlapCounterT;

void print_overlaps(const OverlapCounterT& counter)
{
for(OverlapCounterT::const_iterator it = counter.begin(); it != counter.end(); it++)
{
discrete_interval<int> itv = (*it).first;
int overlaps_count = (*it).second;
if(overlaps_count == 1)
cout << "in interval " << itv << " intervals do not overlap" << endl;
else
cout << "in interval " << itv << ": "<< overlaps_count << " intervals overlap" << endl;
}
}

void overlap_counter()
{
OverlapCounterT overlap_counter;
discrete_interval<int> inter_val;

inter_val = discrete_interval<int>::right_open(4,9);
cout << "adding " << inter_val << endl;
overlap_counter += make_pair(inter_val, 1);
print_overlaps(overlap_counter);

inter_val = discrete_interval<int>::right_open(6,9);
cout << "adding " << inter_val << endl;
overlap_counter += make_pair(inter_val, 1);
print_overlaps(overlap_counter);


inter_val = discrete_interval<int>::right_open(1,9);
cout << "adding " << inter_val << endl;
overlap_counter += make_pair(inter_val, 1);
print_overlaps(overlap_counter);


}

int main()
{
cout << ">>Interval Container Library: Sample overlap_counter.cpp <<\n";
cout << "-----------------------------------------------------------\n";
overlap_counter();
return 0;
}

输出是:

adding   [4,9)

in interval [4,9) intervals do not overlap

adding [6,9)

in interval [4,6) intervals do not overlap

in interval [6,9): 2 intervals overlap

adding [1,9)

in interval [1,4) intervals do not overlap

in interval [4,6): 2 intervals overlap

in interval [6,9): 3 intervals overlap

类似地,在上面的代码中,我将 split_interval_map 更改为 interval_map & 输出是相同的。有什么我想念的吗?

最佳答案

当您插入紧邻的间隔时,差异会变得明显。 “正常”贴图会将它们合并,不会拆分(因此它会保留所有间隔边界)。

另请参阅相关文档:Combining Styles

示例:

Live On Coliru

#include <iostream>
#include <boost/icl/split_interval_map.hpp>
#include <boost/icl/interval_map.hpp>
namespace icl = boost::icl;

template <typename Map> void test() {
std::cout << __PRETTY_FUNCTION__ << ":\n";

Map s { { Map::interval_type::right_open(2,10), 3 } };
std::cout << s << "\n";

s.add({ {10,12}, 3 });
std::cout << s << "\n";
}

int main()
{
test<icl::interval_map<int, int> >();
test<icl::split_interval_map<int, int> >();
}

打印

void test() [with Map = boost::icl::interval_map<int, int>]:
{([2,10)->3)}
{([2,12)->3)}
void test() [with Map = boost::icl::split_interval_map<int, int>]:
{([2,10)->3)}
{([2,10)->3)([10,12)->3)}

关于c++ - boost interval_map 与 split_interval_map,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32075041/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com