Ticket #7231: test.cpp

File test.cpp, 2.7 KB (added by Jess Morecroft <jess.morecroft@…>, 10 years ago)
Line 
1// The next line includes <boost/date_time/posix_time/posix_time.hpp>
2// and a few lines of adapter code.
3#include <boost/icl/ptime.hpp>
4#include <iostream>
5#include <boost/icl/interval_map.hpp>
6
7using namespace std;
8using namespace boost::posix_time;
9using namespace boost::icl;
10
11// Type set<string> collects the names of party guests. Since std::set is
12// a model of the itl's set concept, the concept provides an operator +=
13// that performs a set union on overlap of intervals.
14typedef std::set<string> GuestSetT;
15
16void boost_party()
17{
18 GuestSetT mary_harry;
19 mary_harry.insert("Mary");
20 mary_harry.insert("Harry");
21
22 GuestSetT diana_susan;
23 diana_susan.insert("Diana");
24 diana_susan.insert("Susan");
25
26 GuestSetT peter;
27 peter.insert("Peter");
28
29 // A party is an interval map that maps time intervals to sets of guests
30 interval_map<ptime, GuestSetT> party;
31
32 party.add( // add and element
33 make_pair(
34 interval<ptime>::right_open(
35 time_from_string("2008-05-20 19:30"),
36 time_from_string("2008-05-20 23:00")),
37 mary_harry));
38
39 party += // element addition can also be done via operator +=
40 make_pair(
41 interval<ptime>::right_open(
42 time_from_string("2008-05-20 20:10"),
43 time_from_string("2008-05-21 00:00")),
44 diana_susan);
45
46 party +=
47 make_pair(
48 interval<ptime>::right_open(
49 time_from_string("2008-05-20 22:15"),
50 time_from_string("2008-05-21 00:30")),
51 peter);
52
53
54 interval_map<ptime, GuestSetT>::iterator it = party.begin();
55 cout << "----- History of party guests -------------------------\n";
56 while(it != party.end())
57 {
58 interval<ptime>::type when = it->first;
59 // Who is at the party within the time interval 'when' ?
60 GuestSetT who = (*it++).second;
61 cout << when << ": " << who << endl;
62 }
63
64}
65
66
67int main()
68{
69 cout << ">>Interval Container Library: Sample boost_party.cpp <<\n";
70 cout << "-------------------------------------------------------\n";
71 boost_party();
72 return 0;
73}
74
75// Program output:
76/*-----------------------------------------------------------------------------
77>>Interval Container Library: Sample boost_party.cpp <<
78-------------------------------------------------------
79----- History of party guests -------------------------
80[2008-May-20 19:30:00, 2008-May-20 20:10:00): Harry Mary
81[2008-May-20 20:10:00, 2008-May-20 22:15:00): Diana Harry Mary Susan
82[2008-May-20 22:15:00, 2008-May-20 23:00:00): Diana Harry Mary Peter Susan
83[2008-May-20 23:00:00, 2008-May-21 00:00:00): Diana Peter Susan
84[2008-May-21 00:00:00, 2008-May-21 00:30:00): Peter
85-----------------------------------------------------------------------------*/