boost interval_map是否具有运算符[]或.at()方法? [英] Does boost interval_map have operator [] or .at() method?

查看:112
本文介绍了boost interval_map是否具有运算符[]或.at()方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用BOOST库中的 interval_map .

I'm using interval_map from BOOST library.

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,

现在,我想找出映射到某个数字上的内容:我希望像这样:

Now I want to find out what is mapped on some number: I would expect something like:

cout << populations[1006313];  // 1611,1653

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

但是我似乎找不到任何这样的方法.

However I seem not to find any such a method.

我真的需要将另一个间隔图定义为窗口"并进行交集吗?像这样:

Do I really need to define anoher interval map as "window" and do intersection? Something like:

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

推荐答案

否,boost::icl::interval_map不包含这些元素访问函数.但是,您可以使用 find 函数来完成所需的操作.

No, the boost::icl::interval_map doesn't contain these element access functions. However you can do what you want, using the find function.

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,您将需要其他运算符:

The code above will give you interval, which contains the number 1006313. In order to send std::set<int> to the cout you'll need additional operator:

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

然后下面的行将打印您想要的内容:

Then the line below will print what you want:

cout << it->second << endl;

这篇关于boost interval_map是否具有运算符[]或.at()方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆