[英]Which boost library should i use to add the intervals as mentioned in the description?
I am working on intervals in a c++ program.我正在 C++ 程序中处理间隔。 I want something like below:
我想要像下面这样的东西:
I want to add intervals iteratively in a for loop.我想在 for 循环中迭代地添加间隔。 Assume my first interval is (0, 5).
假设我的第一个区间是 (0, 5)。 I want to add an interval (3,6) such that the resulting interval set should be (0,3), (3,6).
我想添加一个间隔 (3,6),这样生成的间隔集应该是 (0,3), (3,6)。 If my third interval added is (4,7), my resulting interval set should be (0,3), (3,4), (4,7).
如果我添加的第三个间隔是 (4,7),我的结果间隔集应该是 (0,3)、(3,4)、(4,7)。
Any idea what type of interval container should i use from boost library?知道我应该使用 boost 库中的什么类型的间隔容器吗? Any sample programs?
有示例程序吗?
This is what I have tried.....这是我尝试过的......
int main()
{
icl::interval_map<double, std::string> add_map;
using ival = icl::interval<double>;
add_map.add({ival::open(1., 2.5), "A1"});
std::cout<<"adding first interval-----"<<"\n";
for(auto iter : add_map)
std::cout << iter.first << ": " << iter.second << ", "; std::cout << "\n";
add_map.add({ival::open(1.5, 5.), "B1"});
std::cout<<"adding second interval-----"<<"\n";
for(auto iter : add_map)
std::cout << iter.first << ": " << iter.second << ", "; std::cout << "\n";
//after adding second interval, i have to get something like (1,1.5]: A1, (1.5,5): B1
return 0;
}
But I am getting the following output : adding first interval----- (1,2.5): A1,但我得到以下输出:添加第一个间隔-----(1,2.5):A1,
adding second interval----- (1,1.5]: A1, (1.5,2.5): A1B1, [2.5,5): B1,添加第二个间隔----- (1,1.5]: A1, (1.5,2.5): A1B1, [2.5,5): B1,
That is how interval_map
works: it splits on overlap and creates new intervals.这就是
interval_map
工作原理:它在重叠时拆分并创建新的间隔。 This makes queries for all items given an interval or an element efficient, see this doc page .这使得对给定间隔或元素的所有项目的查询有效,请参阅此文档页面。
What bothers me is that in the text you mention integers, but in the code you use double
.令我困扰的是,在文本中您提到了整数,但在代码中您使用了
double
。
Keep in mind that double
intervals may have problems in the limits.请记住,
double
间隔可能会在限制中出现问题。 For example [2, 2.5)
might intersect with (2.5, 3)
or not, depending on the rounding.例如,
[2, 2.5)
可能与(2.5, 3)
相交,也可能不相交,这取决于舍入。 See this post Basic use of function "contains" in Boost ICL: Are some combinations of interval types and functions not implemented?请参阅这篇文章Boost ICL 中函数“包含”的基本使用:是否未实现区间类型和函数的某些组合? .
.
If you just want to store intervals, the data structure I think you need is an interval set .如果您只想存储间隔,我认为您需要的数据结构是interval set 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.