2017-02-20 1 views
1

ユニークでないキーまたは値を持つ双方向コンテナが必要です。マルチマップは単方向であり、boost :: :: bimapはユニークなキーと値しか許さないので動作しません。標準の中のそのようなコンテナや、それ以外の場合は、そのような構造を実装するためのポインタ/記事があれば幸いです。最大100個程度の要素ペアしか期待できません。何か助けていただきありがとうございます、Jeanetteユニークでないキーまたは値を持つ双方向マップを検索する

答えて

0

実際に後押し:: 2領域マップは非ユニークキーを許可しない:

http://www.boost.org/doc/libs/release/libs/bimap/doc/html/boost_bimap/the_tutorial/differences_with_standard_maps.html

例:

#include <boost/bimap.hpp> 
#include <boost/bimap/multiset_of.hpp> 
#include <string> 
#include <iostream> 

namespace bm = boost::bimaps; 

int main() 
{ 
    using mybimap = bm::bimap< bm::multiset_of<std::string>, int >; 
    mybimap map; 

    map.left.insert(mybimap::left_value_type("orange", 1)); 
    map.left.insert(mybimap::left_value_type("apple", 42)); 
    map.left.insert(mybimap::left_value_type("orange", 7));  

    auto rng = map.left.equal_range("orange"); 
    for(auto it = rng.first; it != rng.second; ++it) 
     std::cout << it->first << ": " << it->second << "\n"; 
} 

ライブデモ:

http://coliru.stacked-crooked.com/a/2efdc80cde5f2933

2

ペアのベクトルを使用することができます。 100要素しかない場合、線形検索は両方のペア要素のルックアップに完全に適しています。より大きなコレクションがある場合、またはいずれかのペア要素で同じ範囲が頻繁に必要な場合は、別々のインデックス構造を維持できます。

(これはとにかくどのように実装されるかboost.bimap基本的である。)

関連する問題