質問

交換したいと思っています vector<string> そしてa boost::unordered_map<string, size_t> 前者のインデックスに文字列をマッピングします boost::bimap.

どのようなインスタンス化 bimap 使用する必要がありますか?これまでのところ、私は思いつきました

typedef bimap<
    unordered_set_of<size_t>,
    vector_of<string>
> StringMap;

しかし、コレクションタイプを今逆転させたかどうかはわかりません。また、変更する必要があるのだろうか 関係タイプのコレクション. 。 a vector_of_relation 私の最良の選択、または set_of_relation, 、またはデフォルトで行くだけですか?

役に立ちましたか?

解決

size_tとstd :: stringの間のbimapを取得するには、〜定数(ハッシュのコストと潜在的な衝突のコストまで)を使用する必要があります。

#include <boost/bimap.hpp>
#include <boost/bimap/unordered_set_of.hpp>
#include <string>
#include <iostream>
#include <typeinfo>

int main(int argc, char* argv[]) {

  typedef boost::bimap< boost::bimaps::unordered_set_of<size_t>, boost::bimaps::unordered_set_of<std::string> > StringMap;
  StringMap map;
  map.insert(StringMap::value_type(1,std::string("Cheese")));
  map.insert(StringMap::value_type(2,std::string("Cheese2")));

  typedef StringMap::left_map::const_iterator const_iter_type;
  const const_iter_type end = map.left.end();

  for ( const_iter_type iter = map.left.begin(); iter != end; iter++ ) {
    std::cout << iter->first << " " << map.left.at(iter->first) << "\n";
  }

}

戻り値:

1 Cheese
2 Cheese2

UNORDERED_SETは、ツリーの代わりにハッシュテーブルを使用して要素を保存するセットのブーストバージョンです。 順序付けられていないドキュメントをブーストします.

でbimapの例の1つからのコメントを見る bimapの例, 、 我々は持っています:

左のマップビューはstd :: unordered_map <std :: string、long>のように機能します。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top