简体   繁体   中英

Which boost library should i use to add the intervals as mentioned in the description?

I am working on intervals in a c++ program. I want something like below:

I want to add intervals iteratively in a for loop. Assume my first interval is (0, 5). I want to add an interval (3,6) such that the resulting interval set should be (0,3), (3,6). If my third interval added is (4,7), my resulting interval set should be (0,3), (3,4), (4,7).

Any idea what type of interval container should i use from boost library? 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,

adding second interval----- (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. 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 .

Keep in mind that double intervals may have problems in the limits. For example [2, 2.5) might intersect with (2.5, 3) or not, depending on the rounding. See this post Basic use of function "contains" in Boost ICL: Are some combinations of interval types and functions not implemented? .

If you just want to store intervals, the data structure I think you need is an interval set .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM