boost interval_map有operator[]或.at()方法吗?

9

我正在使用BOOST库中的interval_map

typedef set<int> Tpopulations;    
interval_map<int, Tpopulations> populations;

假设我有这个人口

[1006311,1006353)   1611,1653,
[1006353,1006432)   1031,1611,1653,
[1006432,1006469]   1031,1387,1523,1611,1653,
(1006469,1006484]   1031,1387,1611,1653,
(1006484,1006496]   1031,1387,1611,
(1006496,1006506]   1031,1611,
(1006506,1006547]   1031,

现在我想找出某个数字映射的内容:我期望得到类似于以下内容:
cout << populations[1006313];  // 1611,1653

或者

cout << populations.at(1006313);  // 1611,1653

但是我似乎找不到这样的方法。

我真的需要定义另一个区间映射作为“窗口”并进行交集吗?类似于:

interval_map<int, Tpopulations> window;
set<int>empty_set;
window +=(make_pair(1006313,empty_set));
cout << populations & window

真的没有人回答吗?拜托,我真的需要一些答案 :-/ - user2848463
2个回答

9
不,boost::icl::interval_map 没有这些元素访问函数。不过您可以使用 find 函数实现您的需求。
typedef std::set<int> Tpopulations;
typedef boost::icl::interval_map<int, Tpopulations> IMap;
typedef boost::icl::interval<int> Interval;
...
IMap m;
m += std::make_pair(Interval::right_open(1006311, 1006353), Tpopulations({1611, 1653}));
...
IMap::const_iterator it = m.find(1006313);
cout << it->first << endl;
...

上面的代码将给你一个区间,其中包含数字1006313。为了将std::set<int>发送到cout,您需要使用额外的运算符:
inline std::ostream& operator<< (std::ostream& S, const Tpopulations& X)
{
  S << '(';
  for (ISet::const_iterator it = X.cbegin(); it != X.cend(); ++it)
  {
    if (it != X.cbegin()) S << ',';
    S << *it;
  }
  S << ')';
  return S;
}

然后下面这行代码将会输出你想要的内容:
cout << it->second << endl;

谢谢! 无论如何,我不知怎么错过了返回迭代器的查找方法。 - user2848463

2

是的,一个简单的解决方案是使用()找到您映射的元素。但是,为了这样做,您必须将您的map trait设置为total_absorber,以覆盖整个范围。以下是代码:

interval_map<int, Tpopulations, icl::total_absorber> populations;
Tpopulations valSet = populations(1006313);

你需要遍历valSet或者像@HEKTO上面提到的那样重写operator<<来实际打印出你的数据。 查看区间图选择的boost文档。 这种方法还可以获得最佳预期性能O(log(N))。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接