2

I need to map some intervals (actually these are intervals of addresses) to object ids.

I tried to use boost's interval_map, the example looks very pretty, it easily enumerates all intervals like:

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;
}

Which outputs:

    ----- 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

but it cannot do something like this:

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;

it outputs: error: no match for 'operator[]' in 'party[when]' it looks strange, since the main function of map is in operator[]

so I cannot get information "who were at the party at a given time"

Is there a ready-to-use solution for this problem?

  • Thank you so much, Cv_and_he! Your both solutions do work. – Semyon Semyonych Gorbunkov Aug 10 '13 at 08:55
  • 1
    the correct answer was given by stackexchange's user cv_and_he: `interval::type when = interval::closed( time_from_string("2008-05-20 22:00"), time_from_string("2008-05-20 22:01")); interval_map::const_iterator iter = party.find(when); cout << iter->first << ": " << iter->second << std::endl;` – Semyon Semyonych Gorbunkov Aug 10 '13 at 09:08
  • 1
    and second his (cv_and_he's) correct solution was: `GuestSetT who2 = party(time_from_string("2008-05-20 22:00")); cout << "2008-05-20 22:00 : " << who2 << endl;` – Semyon Semyonych Gorbunkov Aug 10 '13 at 09:10
  • I wasn't sure that it was correct/what you wanted. You can put that as an answer (maybe including the rest of the code for context) and I'll upvote it. – llonesmiz Aug 10 '13 at 10:04

1 Answers1

2

It's somewhat counter-intuitive, but the () operator is what you're looking for. From the docs, operator() is defined as "Return[ing] the mapped value for a key x. The operator is only available for total maps."

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

DickNixon
  • 151
  • 2
  • 7