8

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

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

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

しかし、今コレクションタイプを逆にしたかどうかはわかりません。また、リレーションタイプのコレクションを変更する必要があるのではないかと思います。vector_of_relation私の最良の選択でしょうかset_of_relation、それともデフォルトで行くのでしょうか?

4

1 に答える 1

4

size_tとstd::stringの間のバイマップを取得するには、〜constant(ハッシュと潜在的な衝突のコストまで)がある場合、unordered_set_ofを使用する必要があります。

#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は、ツリーの代わりにハッシュテーブルを使用して要素を格納するセットのブーストバージョンです。BoostUnorderedドキュメントを参照してください。

Bimapの例のbimapの例の1つからのコメントを見ると、次のようになっています。

左側のマップビューは、std :: unordered_map <std :: string、long>のように機能します。国の名前を指定すると、一定時間で人口を検索するために使用できます。

于 2011-06-03T21:21:21.600 に答える