Programming

정적 std :: map 초기화

procodes 2020. 2. 19. 22:17
반응형

정적 std :: map 초기화 C ++에서


정적지도를 초기화하는 올바른 방법은 무엇입니까? 초기화 할 정적 함수가 필요합니까?


C ++ 11 사용하기 :

#include <map>
using namespace std;

map<int, char> m = {{1, 'a'}, {3, 'b'}, {5, 'c'}, {7, 'd'}};

Boost.Assign 사용 :

#include <map>
#include "boost/assign.hpp"
using namespace std;
using namespace boost::assign;

map<int, char> m = map_list_of (1, 'a') (3, 'b') (5, 'c') (7, 'd');

가장 좋은 방법은 함수를 사용하는 것입니다.

#include <map>

using namespace std;

map<int,int> create_map()
{
  map<int,int> m;
  m[1] = 2;
  m[3] = 4;
  m[5] = 6;
  return m;
}

map<int,int> m = create_map();

부스트와 비슷한 것을 만드는 것은 복잡한 문제가 아닙니다. 다음은 생성 기능을 포함하여 부스트가 수행 한 작업을 거의 3 가지만 수행하는 클래스입니다.

template <typename T, typename U>
class create_map
{
private:
    std::map<T, U> m_map;
public:
    create_map(const T& key, const U& val)
    {
        m_map[key] = val;
    }

    create_map<T, U>& operator()(const T& key, const U& val)
    {
        m_map[key] = val;
        return *this;
    }

    operator std::map<T, U>()
    {
        return m_map;
    }
};

용법:

std :: map mymap = create_map <int, int> (1,2) (3,4) (5,6);

위의 코드는 전역 변수 또는 클래스의 정적 멤버를 초기화하는 데 가장 적합하며 초기화 해야하는 시점을 모르지만 값을 사용할 수 있는지 확인하려고합니다.

말하자면, 기존 std :: map에 요소를 삽입해야합니다 ... 여기에 다른 클래스가 있습니다.

template <typename MapType>
class map_add_values {
private:
    MapType mMap;
public:
    typedef typename MapType::key_type KeyType;
    typedef typename MapType::mapped_type MappedType;

    map_add_values(const KeyType& key, const MappedType& val)
    {
        mMap[key] = val;
    }

    map_add_values& operator()(const KeyType& key, const MappedType& val) {
        mMap[key] = val;
        return *this;
    }

    void to (MapType& map) {
        map.insert(mMap.begin(), mMap.end());
    }
};

용법:

typedef std::map<int, int> Int2IntMap;
Int2IntMap testMap;
map_add_values<Int2IntMap>(1,2)(3,4)(5,6).to(testMap);

여기에서 GCC 4.7.2와 함께 작동하는 것을 참조하십시오 : http://ideone.com/3uYJiH

############### 아래의 모든 내용은 폐물입니다 ################

편집 : map_add_values내가 제안한 원래 솔루션 인 아래 클래스는 GCC 4.5 이상에서 실패합니다. 기존지도 값을 추가 하는 방법은 위의 코드를 참조 하십시오 .


template<typename T, typename U>
class map_add_values
{
private:
    std::map<T,U>& m_map;
public:
    map_add_values(std::map<T, U>& _map):m_map(_map){}
    map_add_values& operator()(const T& _key, const U& _val)
    {
        m_map[key] = val;
        return *this;
    }
};

용법:

std :: map <int, int> my_map;
// 나중에 코드를 따라
map_add_values ​​<int, int> (my_map) (1,2) (3,4) (5,6);

참고 : 이전 operator []에는 실제 값을 추가하는 데 사용했습니다 . Dalle의 의견으로는 불가능합니다.

#################### 폐기 섹션의 끝 ####################


다음은 2 요소 데이터 생성자를 사용하는 다른 방법입니다. 초기화에 필요한 기능이 없습니다. 타사 코드 (부스트), 정적 함수 또는 객체, 트릭 없음, 간단한 C ++이 없습니다.

#include <map>
#include <string>

typedef std::map<std::string, int> MyMap;

const MyMap::value_type rawData[] = {
   MyMap::value_type("hello", 42),
   MyMap::value_type("world", 88),
};
const int numElems = sizeof rawData / sizeof rawData[0];
MyMap myMap(rawData, rawData + numElems);

이 답변을 쓴 이후 C ++ 11이 나왔습니다. 새로운 초기화 목록 기능을 사용하여 STL 컨테이너를 직접 초기화 할 수 있습니다.

const MyMap myMap = { {"hello", 42}, {"world", 88} };

정적 객체 안에 맵을 래핑하고 맵 초기화 코드를이 오브젝트의 생성자에 넣습니다. 이렇게하면 초기화 코드가 실행되기 전에 맵이 생성됩니다.


예를 들면 다음과 같습니다.

const std::map<LoggerLevel, const char*> g_logLevelsDescriptions =
{
    { LoggerLevel::llNothing, "Logging disabled"            },
    { LoggerLevel::llInfo,    "Base information"            },
    { LoggerLevel::llWarn,    "Warnings"                    },
    { LoggerLevel::llError,   "Errors"                      },
    { LoggerLevel::llDebug,   "All information: debug-mode" }
};

순수한 C ++ 98 해결 방법을 공유하고 싶었습니다.

#include <map>

std::map<std::string, std::string> aka;

struct akaInit
{
    akaInit()
    {
        aka[ "George" ] = "John";
        aka[ "Joe" ] = "Al";
        aka[ "Phil" ] = "Sue";
        aka[ "Smitty" ] = "Yando";
    }
} AkaInit;

당신은 시도 할 수 있습니다:

std::map <int, int> mymap = 
{
        std::pair <int, int> (1, 1),
        std::pair <int, int> (2, 2),
        std::pair <int, int> (2, 2)
};

PierreBdR지도를 복사하지 않고 와 비슷합니다 .

#include <map>

using namespace std;

bool create_map(map<int,int> &m)
{
  m[1] = 2;
  m[3] = 4;
  m[5] = 6;
  return true;
}

static map<int,int> m;
static bool _dummy = create_map (m);

C ++ 98에 갇혀 있고 부스트를 사용하지 않으려는 경우 정적 맵을 초기화해야 할 때 사용하는 솔루션이 있습니다.

typedef std::pair< int, char > elemPair_t;
elemPair_t elemPairs[] = 
{
    elemPair_t( 1, 'a'), 
    elemPair_t( 3, 'b' ), 
    elemPair_t( 5, 'c' ), 
    elemPair_t( 7, 'd' )
};

const std::map< int, char > myMap( &elemPairs[ 0 ], &elemPairs[ sizeof( elemPairs ) / sizeof( elemPairs[ 0 ] ) ] );

여기에 아주 좋은 답변이 있지만, 나에게있어, "당신이 아는 모든 것이 망치 일 때"의 경우처럼 보입니다 ...

정적지도를 초기화하는 표준 방법이없는 이유에 대한 가장 간단한 대답은 정적지도를 사용해야 할 이유가 없다는 것입니다.

맵은 알려지지 않은 요소 집합을 빠르게 조회하도록 설계된 구조입니다. 미리 요소를 알고 있다면 간단히 C- 어레이를 사용하십시오. 정렬 된 방식으로 값을 입력하거나이를 수행 할 수없는 경우 정렬을 실행하십시오. 그런 다음 stl :: functions를 사용하여 항목 lower_bound / upper_bound를 루프 업하여 log (n) 성능을 얻을 수 있습니다. 내가 이전에 이것을 테스트했을 때, 그들은 보통지도보다 적어도 4 배 더 빨리 수행합니다.

장점은 여러 가지입니다.-더 빠른 성능 (* 4, 많은 CPU 유형에서 측정 한 결과 항상 약 4입니다)-더 간단한 디버깅. 선형 레이아웃으로 무슨 일이 일어나고 있는지 쉽게 알 수 있습니다. -필요한 복사 작업의 간단한 구현. -런타임에 메모리를 할당하지 않으므로 예외가 발생하지 않습니다. -표준 인터페이스이므로 DLL, 언어 등을 공유하기가 매우 쉽습니다.

나는 계속할 수 있지만 더 많은 것을 원한다면 Stroustrup의 많은 블로그를 주제로 살펴보십시오.

참고 URL : https://stackoverflow.com/questions/138600/initializing-a-static-stdmapint-int-in-c



반응형