C ++中的区间映射

时间:2013-08-10 07:49:06

标签: boost map interval-tree

我需要将一些间隔(实际上是地址间隔)映射到对象ID。

我尝试使用boost的interval_map,这个例子看起来非常漂亮,它很容易枚举所有的区间,如:

while(it != party.end())
{
    interval<ptime>::type when = it->first;
    // Who is at the party within the time interval 'when' ?
    GuestSetT who = (*it++).second;
    cout << when << ": " << who << endl;
}

哪个输出:

    ----- History of party guests -------------------------
    [2008-May-20 19:30:00, 2008-May-20 20:10:00): Harry Mary
    [2008-May-20 20:10:00, 2008-May-20 22:15:00): Diana Harry Mary Susan
    [2008-May-20 22:15:00, 2008-May-20 23:00:00): Diana Harry Mary Peter Susan
    [2008-May-20 23:00:00, 2008-May-21 00:00:00): Diana Peter Susan
    [2008-May-21 00:00:00, 2008-May-21 00:30:00): Peter

但它无法做到这样的事情:

interval<ptime>::type when = 
    interval<ptime>::closed(
        time_from_string("2008-05-20 22:00"),
        time_from_string("2008-05-20 22:01"));

    GuestSetT who = party[when];

    cout << when << ": " << who << endl;

输出:错误:不匹配&#39;运营商[]&#39;在#party; party [when]&#39; 它看起来很奇怪,因为map的主要功能是在operator []

所以我无法获得信息&#34;谁在某个时间参加聚会&#34;

这个问题是否有现成的解决方案?

1 个答案:

答案 0 :(得分:1)

这有点违反直觉,但是()运算符是你正在寻找的。从文档中,operator()被定义为“返回键x的映射值。运算符仅适用于总映射。”

来源:http://www.boost.org/doc/libs/1_54_0/libs/icl/doc/html/boost_icl/function_reference/selection.html