初始化一个静态 std::map<int, int>在 C++ 中 [英] Initializing a static std::map&lt;int, int&gt; in C++

查看:28
本文介绍了初始化一个静态 std::map<int, int>在 C++ 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

初始化静态地图的正确方法是什么?我们是否需要一个静态函数来初始化它?

What is the right way of initializing a static map? Do we need a static function that will initialize it?

推荐答案

使用 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');

这篇关于初始化一个静态 std::map<int, int>在 C++ 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆