【问题标题】:removing discrete closed intervals from boost::icl::interval_set从 boost::icl::interval_set 中删除离散的闭区间
【发布时间】:2016-01-20 20:21:39
【问题描述】:
  boost::icl::interval_set<uint> test_set;
  test_set.insert(boost::icl::discrete_interval<uint>::closed(10u, 20u));
  test_set.insert(boost::icl::discrete_interval<uint>::closed(21u, 30u)); //should merge to single interval
  test_set.insert(boost::icl::discrete_interval<uint>::closed(15u, 25u)); //should not change
  test_set.erase(boost::icl::discrete_interval<uint>::closed(12u, 18u)); //should split in two intervals
  uint i1min = test_set.begin()->lower();
  uint i1max = test_set.begin()->upper();
  uint i2min = (++test_set.begin())->lower();
  uint i2max = (++test_set.begin())->upper();
  std::cout<<i1min<<"\n";
  std::cout<<i1max<<"\n";
  std::cout<<i2min<<"\n";
  std::cout<<i2max<<"\n";

由于我添加和减去闭合区间,我希望得到以下输出:

10
11
19
30

但我得到了:

10
12
18
30

为什么我要删除的时间间隔的端点仍然存在?这是减去闭合间隔的预期行为还是我做错了什么?

【问题讨论】:

    标签: c++ boost intervals


    【解决方案1】:

    从另一个中减去一个封闭区间,在您的情况下会导致两个半开区间。如果您考虑一下,它也与连续间隔的行为一致。

    您可以使用以下代码对此进行测试:

     std::cout << test_set.begin()->bounds() << "\n";
     std::cout << (++test_set.begin())->bounds() << "\n";
    

    此输出:[)(] 表明生成的区间是半开的,这意味着 1218 不包含在您的区间集中。

    要获取(可能是开放的)离散区间的第一个/最后一个元素,请使用函数firstlast(文档here):

    uint i1min = boost::icl::first(*test_set.begin());
    uint i1max = boost::icl::last(*test_set.begin());
    uint i2min = boost::icl::first(*(++test_set.begin()));
    uint i2max = boost::icl::last(*(++test_set.begin()));
    

    【讨论】:

    • 哦,我明白了,这是有道理的!在那种情况下,有没有办法获得仍然是区间一部分的离散区间的最高元素?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-03
    • 2015-06-20
    • 1970-01-01
    • 1970-01-01
    • 2010-10-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多