문제

내 응용 프로그램에서는 많은 정적 객체에 해시 맵 매핑 문자열이 필요합니다. 매핑은 응용 프로그램 기간 동안 고정되어 있습니다. 애플리케이션이 시작될 때 요소 별 요소를 구축하는 대신 컴파일 시간에 매핑을 미리 생성하는 쉬운 방법이 있습니까?

도움이 되었습니까?

해결책

찾다 gperf, 그것은 완벽하게 해시 할 코드를 생성합니다.

다른 팁

BurtleBob의 완벽한 해싱 기능을 확인하십시오. 내 경험상 그것은 gperf보다 유연합니다. http://burtleburtle.net/bob/hash/perfect.html

매핑과 함께 헤더 파일을 방출하는 간단한 코드 생성기를 작성하고 빌드 프로세스의 사전 구축 단계로 실행할 수 있습니다.

당신은 찾고 있습니다 boost.assign 's 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