Home >Backend Development >C++ >How Can I Initialize a Static `std::map` in C ?
Initialization of Static std::map
When initializing a static std::map
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!