How to Initialize a Static std::map<int, int> in C++
Last Updated :
02 Aug, 2024
In C++, std::map<int, int> is a commonly used container that stores key-value pairs. There are scenarios where we may want to initialize a static std::map with predefined key-value pairs that remain constant throughout the program's execution. In this article, we will learn different methods to initialize a static std::map<int, int> in C++.
Initialize std::map<int, int> in C++
There are mainly three ways to initialize a static std::map<int, int> in C++:
- With Declaration Using Initializer List
- Using a Static Member Function
- Using a Static Initialization Block
1. Initialize with Declaration Using Initializer List
The simplest way to initialize a static std::map<int, int> is at the time of declaration by using an initializer list introduced in C++ 11 .
Syntax:
static const std::map<int, int> myMap = {
{key1, value1},
{key2, value2},
...
{keyN, valueN}
};
Example:
C++
// C++ program to demonstrate initialization of a static
// std::map<int, int> using initializer list
// Include necessary header files
#include <iostream>
#include <map>
using namespace std;
// Initialize a static const map with some key-value pairs
static const map<int, int> myMap
= { { 1, 10 }, { 2, 20 }, { 3, 30 }, { 4, 40 } };
int main()
{
// Iterate through the map and print each key-value pair
for (const auto& pair : myMap) {
cout << "Key: " << pair.first
<< ", Value: " << pair.second << endl;
}
return 0;
}
OutputKey: 1, Value: 10
Key: 2, Value: 20
Key: 3, Value: 30
Key: 4, Value: 40
2. Using a Static Member Function
Another method is to use a static member function that returns a pre-initialized std::map. This approach is useful when dealing with complex initialization logic.
Syntax:
class MapInitializer {
public:
static const std::map<int, int>& getMap() {
static const std::map<int, int> myMap = {
{key1, value1},
{key2, value2},
...
{keyN, valueN}
};
return myMap;
}
};
Example:
C++
// C++ program to demonstrate initialization of a static
// std::map<int, int> using static member function
// Include necessary header files
#include <iostream>
#include <map>
using namespace std;
// Class to initialize and return a static map
class MapInitializer {
public:
// Static function to return a constant reference to the
// static map
static const map<int, int>& getMap()
{
// Initialize the static map with key-value pairs
static const map<int, int> myMap = {
{ 1, 10 }, { 2, 20 }, { 3, 30 }, { 4, 40 }
};
// Return the map
return myMap;
}
};
int main()
{
// Get the static map from the MapInitializer class
const map<int, int>& myMap = MapInitializer::getMap();
// Iterate through the map and print each key-value pair
for (const auto& pair : myMap) {
cout << "Key: " << pair.first
<< ", Value: " << pair.second << endl;
}
// Return 0 to indicate successful execution
return 0;
}
OutputKey: 1, Value: 10
Key: 2, Value: 20
Key: 3, Value: 30
Key: 4, Value: 40
3. Using a Static Initialization Block
For older versions of C++ that do not support initializer lists, we can use a static initialization block within a function.
Syntax:
static const std::map<int, int>& getMap() {
static std::map<int, int> myMap;
if (myMap.empty()) {
myMap[key1] = value1;
myMap[key2] = value2;
...
myMap[keyN] = valueN;
}
return myMap;
}
Example:
C++
// C++ program to demonstrate initialization of a static
// std::map<int, int> using static initialization block
// Include necessary header files
#include <iostream>
#include <map>
using namespace std;
// Function to initialize and return a reference to a static
// map
static const map<int, int>& getMap()
{
// Initialize the static map
static map<int, int> myMap;
// Check if the map is empty
if (myMap.empty()) {
// Populate the map with key-value pairs
myMap[1] = 10;
myMap[2] = 20;
myMap[3] = 30;
myMap[4] = 40;
}
// Return the map
return myMap;
}
int main()
{
// Get the static map from the getMap function
const map<int, int>& myMap = getMap();
// Iterate through the map and print each key-value pair
for (const auto& pair : myMap) {
cout << "Key: " << pair.first
<< ", Value: " << pair.second << endl;
}
return 0;
}
OutputKey: 1, Value: 10
Key: 2, Value: 20
Key: 3, Value: 30
Key: 4, Value: 40