【问题标题】:Does Boost c++ library interval_map supports remove?Boost c++ 库 interval_map 是否支持删除?
【发布时间】:2013-06-05 01:40:50
【问题描述】:

interval_map(在 icl 中)库是否提供删除支持?我可以根据迭代器查找范围并删除范围吗?

============ boost 示例中的party.cpp ===============

partyp->add( // add and element
  make_pair( 
    interval<ptime>::right_open(
      time_from_string("2008-05-20 19:30"), 
      time_from_string("2008-05-20 23:00")), 
    mary_harry));

party += // element addition can also be done via operator +=
  make_pair( 
    interval<ptime>::right_open(
      time_from_string("2008-05-20 20:10"), 
      time_from_string("2008-05-21 00:00")), 
    diana_susan);

party +=
  make_pair( 
    interval<ptime>::right_open(
      time_from_string("2008-05-20 22:15"), 
      time_from_string("2008-05-21 00:30")), 
    peter);

=========== 我的问题是我可以添加一个删除语句,如

 party -= 
        interval<ptime>::right_open(
          time_from_string("2008-05-20 20:10"), 
          time_from_string("2008-05-21 00:00"));

我只想删除范围。任何方法都可以。

【问题讨论】:

    标签: c++ boost intervals


    【解决方案1】:

    我知道这是一个旧帖子,但我有同样的问题,我终于找到了答案。

    查看docs 我猜想interval_map 的可减性重叠聚合 功能保证了减法作为删除操作。 p>

    结果证明这是一个非常富有成效的概念来传播加法 或在以下情况下减去 interval_map 的关联值 将间隔值对插入到 interval_map 导致 在插入的区间值对与区间值的冲突中 对,已经在 interval_map 中。这个操作 传播称为重叠聚合。

    假设我必须将 unix 时间戳的间隔与一些记录 ID(整数)相匹配。 从answer 工作,我想出了这个 MWE:

    // interval_map_mwe.cpp
    #include <map>
    #include <set>
    #include <climits>
    
    #include <boost/icl/interval.hpp>
    #include <boost/icl/interval_map.hpp>
    
    // Set of IDs that cover a time interval
    typedef std::set<unsigned int> IDSet_t;
    // interval tree from intervals of timestamps to a set of ids
    typedef boost::icl::interval_map<time_t, IDSet_t> IMap_t;
    // a time interval
    typedef boost::icl::interval<time_t> Interval_t;
    
    #include <iostream>
    // from https://stackoverflow.com/a/22027957
    inline std::ostream& operator<< (std::ostream& S, const IDSet_t& X)
    {
        S << '(';
        for (IDSet_t::const_iterator it = X.begin(); it != X.end(); ++it) {
            if (it != X.begin()) {
                S << ',';
            }
            S << *it;
        }
        S << ')';
        return S;
    }
    
    int main(int argc, const char *argv[])
    {
        (void)argc; // suppress warning
        (void)argv; // suppress warning
    
        IMap_t m;
    
        IDSet_t s;
        s.insert(1);
        s.insert(2);
        m += std::make_pair(Interval_t::right_open(100, 200), s);
    
        s = IDSet_t();
        s.insert(3);
        s.insert(4);
        m += std::make_pair(Interval_t::right_open(200, 300), s);
    
        s = IDSet_t();
        s.insert(5);
        s.insert(6);
        m += std::make_pair(Interval_t::right_open(150, 250), s);
    
        std::cout << "Initial map: " << std::endl;
        std::cout << m << std::endl;
    
        // find operation
        IMap_t::const_iterator it = m.find(175);
    
        std::cout << "Interval that covers 175: ";
        std::cout << it->first << std::endl;
        std::cout << "Ids in interval: " << it->second << std::endl;
    
        // partially remove 5 from interval (160,180)
        s = IDSet_t();
        s.insert(5);
        m -= std::make_pair(Interval_t::right_open(160, 180), s);
    
        std::cout << "map with 5 partially removed:" << std::endl;
        std::cout << m << std::endl;
    
        // completelly remove 6
        s = IDSet_t();
        s.insert(6);
        // Note: maybe the range of the interval could be shorter if you can somehow obtain the minimum and maximum times
        m -= std::make_pair(Interval_t::right_open(0, UINT_MAX), s);
    
        std::cout << "map without 6: " << std::endl;
        std::cout << m << std::endl;
    
        // remove a time interval
        m -= Interval_t::right_open(160, 170);
    
        std::cout << "map with time span removed: " << std::endl;
        std::cout << m << std::endl;
    
        return 0;
    }
    

    使用 g++ 4.4.7 编译:

    g++ -Wall -Wextra -std=c++98 -I /usr/include/boost148/ interval_map_mwe.cpp
    

    我得到的输出是

    Initial map:
    {([100,150)->{1 2 })([150,200)->{1 2 5 6 })([200,250)->{3 4 5 6 })([250,300)->{3 4 })}
    Interval that covers 175: [150,200)
    Ids in interval: (1,2,5,6)
    map with 5 partially removed:
    {([100,150)->{1 2 })([150,160)->{1 2 5 6 })([160,180)->{1 2 6 })([180,200)->{1 2 5 6 })([200,250)->{3 4 5 6 })([250,300)->{3 4 })}
    map without 6:
    {([100,150)->{1 2 })([150,160)->{1 2 5 })([160,180)->{1 2 })([180,200)->{1 2 5 })([200,250)->{3 4 5 })([250,300)->{3 4 })}
    map with time span removed:
    {([100,150)->{1 2 })([150,160)->{1 2 5 })([170,180)->{1 2 })([180,200)->{1 2 5 })([200,250)->{3 4 5 })([250,300)->{3 4 })}
    

    注意: MWE 中的数字可以认为是随机的。我发现用小数字来推理这个例子更容易。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-26
      • 2016-08-02
      • 1970-01-01
      • 2017-12-03
      • 1970-01-01
      • 2020-11-21
      • 2011-07-08
      • 2012-11-13
      相关资源
      最近更新 更多