初始化一个静态 std::map<int, int>在 C++ 中

Initializing a static std::maplt;int, intgt; in C++(初始化一个静态 std::mapint, int在 C++ 中)

本文介绍了初始化一个静态 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++ 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:初始化一个静态 std::map<int, int>在 C++ 中

基础教程推荐