2013-10-05 22 views
9

我在BOOST庫中使用interval_map提升interval_map是否有operator []或.at()方法?

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

認真,沒有人回答呢?請,我真的可以使用一些答案: -/ – user2848463

回答

7

沒有,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; 
+0

謝謝!無論如何,不​​知何故,我錯過了返回迭代器的方法。 – user2848463

1

是的,一個簡單的解決方案是找到你的映射元素()。但是,要這樣做,必須設置地圖特徵= total_absorber,以覆蓋整個範圍。下面是代碼:

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

你會然後遍歷valSet或覆蓋operator<<如@HEKTO上述實際打印出您的數據。

Check the boost documents for selection on interval_map. 這種方法也給出了O(日誌(N))的最佳預期表現

相關問題