Trying to follow the boost_party
example of interval_map I made this example code:
#include "boost/icl/interval.hpp"
#include "boost/icl/interval_map.hpp"
#include <set>
using namespace std;
typedef std::set<string> ids;
int main(int argc, char* argv[])
{
ids ids1;
ids1.insert("T1");
ids ids2;
ids2.insert("T2");
boost::icl::interval_map<boost::icl::closed_interval<int>, ids> mymap;
boost::icl::closed_interval<int> i1 = boost::icl::closed_interval<int>(2, 7);
boost::icl::closed_interval<int> i2 = boost::icl::closed_interval<int>(3, 8);
mymap += make_pair(i1, ids1);
mymap += make_pair(i2, ids2);
return 0;
}
But this get's this compilation error:
error: no match for ‘operator+=’ (operand types are ‘boost::icl::interval_map<boost::icl::closed_interval<int>, std::set<std::basic_string<char> > >’ and ‘std::pair<boost::icl::closed_interval<int>, std::set<std::basic_string<char> > >’)
mymap += make_pair(i1, ids1);
Any idea what's wrong?
should most likely be mymap.insert(make_pair(...));
instead of mymap += make_pair(...);
. That's the syntax for std::unordered_map
.
Maybe I am a little late, but this compiles:
#include "boost/icl/interval.hpp"
#include "boost/icl/interval_map.hpp"
#include <set>
using namespace std;
typedef std::set<string> ids;
int main(int argc, char* argv[])
{
ids ids1;
ids1.insert("T1");
ids ids2;
ids2.insert("T2");
boost::icl::interval_map<int, ids> mymap;
auto i1 = boost::icl::interval<int>::closed(2, 7);
auto i2 = boost::icl::interval<int>::closed(3, 8);
mymap += make_pair(i1, ids1);
mymap += make_pair(i2, ids2);
return 0;
}
You would have to check, if this matches your intended semantics, though.
Actually the problem is in your declaration of the interval_map:
boost::icl::interval_map<boost::icl::closed_interval<int>, ids> mymap;
The problem is, that the library automatically wants to create intervals of the first type you give it. So in your case the key of map is interval of interval of integers.
So in your case all you need to do is to use:
boost::icl::interval_map<int, ids> mymap;
In that case your map will be mapping int intervals onto sets of strings.
First, replace +=
by .insert(...)
. (I never understood well when to use +=
or insert
, I use insert
always).
Second, I think the key for the interval_map
is int
and not discrete_interval<int>
. Also, I don't think is up to you whether the intervals are closed or (semi)open. With discrete
intervals unions of open intervals can be become closed intervals. So let the dynamic type system choose for you. The code ends up looking like this (C++98, compiles with g++ 4.8.3
)
typedef std::set<std::string> ids;
ids ids1;
ids1.insert("T1");
ids ids2;
ids2.insert("T2");
boost::icl::interval_map<int, ids> mymap;
boost::icl::discrete_interval<int> i1 = boost::icl::discrete_interval<int>::closed(2, 7);
boost::icl::discrete_interval<int> i2 = boost::icl::discrete_interval<int>::closed(3, 8);
mymap.insert(make_pair(i1, ids1));
mymap.insert(make_pair(i2, ids2));
Curiosity: your code compiles in with -std=c++11
, I don't know if it does the right thing though.