简体   繁体   English

我应该使用哪个 boost 库来添加描述中提到的间隔?

[英]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.

相关问题 我怎么知道我应该使用哪个C ++标准版本来构建哪个版本的Boost? - How do I know which C++ standard version I should use to build which version of Boost? 如何为boost :: program_options的位置选项添加描述? - How to add a description to boost::program_options' positional options? 我应该为我的应用程序和库使用智能指针吗? - Should I use smart pointers for my application and library? 为什么在参数unordered_map之前添加“&”? - Why I should add '&' before the parameter which is an unordered_map? 我应该停止使用抽象基类/接口而是使用boost :: function / std :: function吗? - Should I stop using abstract base classes/interfaces and instead use boost::function/std::function? 尝试学习boost :: intrusive Q3-在IC中存储指针时,我应该使用smart_pointer吗? - Trying to learn boost::intrusive Q3 - When storing pointers in ICs, should I use smart_pointer? For 循环或 std::any_of,我应该使用哪一个? - For-loop or std::any_of, which one should I use? 尝试使用std :: round()时,我应该添加少量吗? - Should I add a tiny amount when trying to use std::round()? 我什么时候应该使用remove_reference和add_reference? - When should I use remove_reference and add_reference? 使用Boost :: Beast来处理CPU繁重的REST API,是否应该使用异步或同步方式来实现它们以期望更好的延迟? - Using Boost::Beast for CPU-heavy REST APIs, Should I use Async or Sync way to implement them to expect the better latency?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM