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
Release: 2024-12-29 17:39:10
Original
754 people have browsed it

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}};
Copy after login

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);
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template