
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Map C++ Enums to Strings
Here we will see how to map enum type data to a string in C++ There is no such direct function to do so. But we can create our own function to convert enum to string. We shall create a function that takes an enum value as the argument, and we manually return the enum names as a string from that function. First of all, let us understand what is an enum in C++.
What is an Enum in C++?
Enum is a user-defined datatype in C++ that is used to assign names to integer values. For example, you can use an enum to represent the days of the week from 0 to 6, where 0 represents Sunday, 1 represents Monday, and so on. When you try to access a value of an enum, it will only return an integer value. So, to access the string value of an enum, we need to map each enum value to its corresponding string representation.
Example of Enum:
enum color { RED, // value 0 GREEN, // value 1 BLUE // value 2 };
Mapping C++ Enum to String
Here are the approaches to map a C++ enum to a string that we will cover along with examples:
Map Enum to String Using Unordered Map
The unordered_map is a part of the C++ Standard Library that stores data in key-value pairs. We can use this map to store the enum values as keys and their corresponding string representations as values. Let's see an example to understand how to do this.
Example
In this example, we will use an unordered_map to map the enum values to their string representations.
#include <iostream> #include <unordered_map> #include <string> using namespace std; enum Color { RED, GREEN, BLUE }; int main() { unordered_map<Color, string> colorMap = { {RED, "Red"}, {GREEN, "Green"}, {BLUE, "Blue"} }; Color color = GREEN; cout << "The color "<< color << " is: " << colorMap[color] << endl; return 0; }
The output of the above code will be:
The color 1 is: Green
Map Enum to String Using constexpr and Arrays
The constexpr is used to define compile-time constants in C++. We can use it along with arrays to map enum values to their string representations. Let's see how to do this with an example.
Example
In this example, we will use constexpr and arrays to map the enum values to their string representations.
#include <iostream> #include <array> #include <string> using namespace std; enum Color { RED, GREEN, BLUE }; constexpr array<const char*, 3> colorNames = {"Red", "Green", "Blue"}; int main() { Color color = BLUE; cout << "The color "<< color << " is: " << colorNames[color] << endl; return 0; }
The output of the above code will be:
The color 2 is: BLUE
Map Enum to String Using Switch Case
The switch statement is a control statement that is used to check if a variable is equal to any value in a list of values. We can use this switch statement to convert an enum type variable to a string type variable by checking each case and returning the corresponding string value.
Example
In this example, we will use a switch case to convert an enum type variable to a string type variable.
#include <iostream> #include <string> enum color { RED, // value 0 GREEN, // value 1 BLUE // value 2 }; std::string colorToString(color c) { switch (c) { case RED: return "RED"; case GREEN: return "GREEN"; case BLUE: return "BLUE"; default: return "UNKNOWN"; } } int main() { color c = GREEN; std::cout << "The color " << c << " is: " << colorToString(c) << std::endl; return 0; }
The output of the above code will be:
The color 1 is: GREEN