C++ 在编译时(C+;+;)创建大型哈希映射的最佳方法?

C++ 在编译时(C+;+;)创建大型哈希映射的最佳方法?,c++,data-structures,hashtable,hashmap,C++,Data Structures,Hashtable,Hashmap,在我的应用程序中,我需要将字符串映射到大量静态对象的哈希映射。映射在应用程序期间保持不变。有没有一种简单的方法可以在编译时预生成映射,而不是在应用程序启动时逐个元素地构建映射 您可以编写一个简单的代码生成器,该生成器发出带有映射的头文件,并在构建过程中作为预构建步骤运行。您可以编写一个简单的代码生成器,该生成器发出带有映射的头文件,并在构建过程中作为预构建步骤运行。您正在寻找的。它也适用于hashmaps #include <boost/assign/list_of.hpp> //

在我的应用程序中,我需要将字符串映射到大量静态对象的哈希映射。映射在应用程序期间保持不变。有没有一种简单的方法可以在编译时预生成映射,而不是在应用程序启动时逐个元素地构建映射

您可以编写一个简单的代码生成器,该生成器发出带有映射的头文件,并在构建过程中作为预构建步骤运行。

您可以编写一个简单的代码生成器,该生成器发出带有映射的头文件,并在构建过程中作为预构建步骤运行。

您正在寻找的。它也适用于hashmaps

#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 );  
}
#包含//for'map_list_of()
#包括
#包括
使用名称空间std;
使用名称空间boost::assign;//将“map\u list\u of()”引入范围
{
map next=map_list_of(1,2)(2,3)(3,4)(4,5)(5,6);
BOOST_断言(next.size()=5);
BOOST_断言(next[1]==2);
BOOST_断言(next[5]==6);
//或者我们可以通过指定类型来使用“list_of()”
//名单包括:
下一步=列表_的(6,7)(7,8)(8,9);
BOOST_断言(next.size()=3);
BOOST_断言(next[6]==7);
BOOST_断言(next[8]==9);
}
您正在寻找的。它也适用于hashmaps

#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 );  
}
#包含//for'map_list_of()
#包括
#包括
使用名称空间std;
使用名称空间boost::assign;//将“map\u list\u of()”引入范围
{
map next=map_list_of(1,2)(2,3)(3,4)(4,5)(5,6);
BOOST_断言(next.size()=5);
BOOST_断言(next[1]==2);
BOOST_断言(next[5]==6);
//或者我们可以通过指定类型来使用“list_of()”
//名单包括:
下一步=列表_的(6,7)(7,8)(8,9);
BOOST_断言(next.size()=3);
BOOST_断言(next[6]==7);
BOOST_断言(next[8]==9);
}

查找,它会为您生成完美散列的代码。

查找,它会为您生成完美散列的代码。

查看Burtlebob的完美散列函数。根据我的经验,它比gperf更灵活

查看Burtlebob的完美哈希函数。根据我的经验,它比gperf更灵活

您可以尝试使用。此解决方案需要一个支持C++-14标准的编译器。

您可以尝试使用。此解决方案需要一个支持C++-14标准的编译器。

在本例中,映射仍然是在运行时生成的。看起来它只是同一个操作的合成糖。在本例中,映射仍然是在运行时生成的。看起来就像是同一道工序的合成糖。