コンパイル時に大きなハッシュマップを作成する最良の方法(C ++)?

StackOverflow https://stackoverflow.com/questions/1412016

質問

私のアプリケーションでは、文字列を多数の静的オブジェクトにマッピングするハッシュマップが必要です。マッピングは、アプリケーションの期間中は固定されたままです。アプリケーションの起動時に要素ごとにマッピングするのではなく、コンパイル時にマッピングを事前生成する簡単な方法はありますか?

役に立ちましたか?

解決

gperf を検索すると、完全にハッシュされるコードが生成されます。

他のヒント

Burtlebobの完璧なハッシュ関数を調べてください。私の経験では、gperfよりも柔軟です。 http://burtleburtle.net/bob/hash/perfect.html

マッピングを含むヘッダーファイルを生成する簡単なコードジェネレーターを作成し、ビルドプロセスのビルド前ステップとして実行できます。

Boostを探しています。割り当ての map_list_of 。ハッシュマップでも機能します。

#include <boost/assign/list_of.hpp> // for 'map_list_of()'
#include <boost/assert.hpp> 
#include <map>
using namespace std;
using namespace boost::assign; // bring 'map_list_of()' into scope

{
    map<int,int> next = map_list_of(1,2)(2,3)(3,4)(4,5)(5,6);
    BOOST_ASSERT( next.size() == 5 );
    BOOST_ASSERT( next[ 1 ] == 2 );
    BOOST_ASSERT( next[ 5 ] == 6 );

    // or we can use 'list_of()' by specifying what type
    // the list consists of
    next = list_of< pair<int,int> >(6,7)(7,8)(8,9);
    BOOST_ASSERT( next.size() == 3 );
    BOOST_ASSERT( next[ 6 ] == 7 );
    BOOST_ASSERT( next[ 8 ] == 9 );  
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top