[英]Initializing a static std::map<int, int> in 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();
做類似boost的東西不是什么復雜的問題。 這是一個只有三個函數的類,包括構造函數,用於復制 boost 所做的(幾乎)。
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; // Later somewhere along the code map_add_values<int,int>(my_map)(1,2)(3,4)(5,6);
注意:以前我使用operator []
來添加實際值。 正如 dalle 所評論的那樣,這是不可能的。
##################### 過時部分結束#####################
這是使用 2 元素數據構造函數的另一種方法。 不需要函數來初始化它。 沒有第 3 方代碼 (Boost),沒有靜態函數或對象,沒有技巧,只有簡單的 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<LogLevel, const char*> g_log_levels_dsc =
{
{ LogLevel::Disabled, "[---]" },
{ LogLevel::Info, "[inf]" },
{ LogLevel::Warning, "[wrn]" },
{ LogLevel::Error, "[err]" },
{ LogLevel::Debug, "[dbg]" }
};
如果 map 是類的數據成員,則可以通過以下方式直接在 header 中對其進行初始化 (C++17 起):
// Example
template<>
class StringConverter<CacheMode> final
{
public:
static auto convert(CacheMode mode) -> const std::string&
{
// validate...
return s_modes.at(mode);
}
private:
static inline const std::map<CacheMode, std::string> s_modes =
{
{ CacheMode::All, "All" },
{ CacheMode::Selective, "Selective" },
{ CacheMode::None, "None" }
// etc
};
};
我會將地圖包裝在一個靜態對象中,並將地圖初始化代碼放在該對象的構造函數中,這樣您就可以確保在執行初始化代碼之前創建了地圖。
只是想分享一個純 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)
};
如果你堅持使用 C++98 並且不想使用 boost,這里有我在需要初始化靜態映射時使用的解決方案:
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 ] ) ] );
這類似於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);
除了使用的最佳答案
const std::map<int, int> m = {{1,1},{4,2},{9,3},{16,4},{32,9}}
通過直接調用在某些情況下有用的 lambda,還有一種可能性:
const std::map<int, int> m = []()->auto {
std::map<int, int> m;
m[1]=1;
m[4]=2;
m[9]=3;
m[16]=4;
m[32]=9;
return m;
}();
很明顯,使用文字值從頭開始編寫時,一個簡單的初始化列表會更好,但它確實開辟了更多的可能性:
const std::map<int, int> m = []()->auto {
std::map<int, int> m;
for(int i=1;i<5;++i) m[i*i]=i;
m[32]=9;
return m;
}();
(顯然,如果您想重新使用它,它應該是一個普通函數;這確實需要最新的 C++。)
你在這里有一些很好的答案,但我對我來說,這看起來像是“當你所知道的只是一把錘子”的情況......
為什么沒有初始化靜態地圖的標准方法的最簡單答案是,沒有充分的理由使用靜態地圖......
映射是為快速查找未知元素集而設計的結構。 如果您事先知道這些元素,只需使用 C 數組即可。 以排序的方式輸入值,或者如果您不能這樣做,則對它們運行排序。 然后,您可以通過使用 stl::functions 循環條目 lower_bound/upper_bound 來獲得 log(n) 性能。 當我之前對此進行測試時,它們通常至少比地圖快 4 倍。
優點有很多... - 更快的性能(*4,我對許多 CPU 類型進行了測量,它總是在 4 左右) - 更簡單的調試。 使用線性布局更容易看到發生了什么。 - 如果有必要,復制操作的簡單實現。 - 它在運行時不分配內存,因此永遠不會拋出異常。 - 這是一個標准接口,因此很容易跨 DLL 或語言等共享。
我可以繼續,但如果您想要了解更多,何不查看 Stroustrup 關於該主題的許多博客。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.