Home >Backend Development >C++ >How Can I Initialize a Static `std::map` in C ?

How Can I Initialize a Static `std::map` in C ?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-12-29 17:39:10726browse

How Can I Initialize a Static `std::map` in C  ?

Initialization of Static std::map in C

When initializing a static std::map in C , there are several approaches to consider.

C 11 Initializer List

Utilizing the C 11 initializer list, you can initialize a static map directly:

#include <map>

using namespace std;

static map<int, int> m = {{1, 2}, {3, 4}, {5, 6}};

Boost.Assign

Alternatively, you can leverage the Boost.Assign library to initialize a map concisely:

#include <map>
#include "boost/assign.hpp"

using namespace std;
using namespace boost::assign;

static map<int, int> m = map_list_of(1, 2)(3, 4)(5, 6);

Note:

Regardless of the approach chosen, static maps are initialized during program startup and remain initialized throughout the program's execution.

The above is the detailed content of How Can I Initialize a Static `std::map` in C ?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn