Table of Contents
There are a variety of methods to convert enum to String in C++. In this article, we will discuss some of the efficient and easy ways to convert enum to string.
enum is a user-defined data type that contains integral constants. We use the keyword ‘enum’ to define it.
Example:
1 2 3 |
enum languages {Java, CPP, Python, Ruby}; |
Here the name of the enum is languages
and {Java, CPP, Python, Ruby} are values of type enum. By default, Java is 0, CPP is 1 and so on. The default values can also be changed at the time of declaration like this:
1 2 3 4 5 6 7 8 |
enum languages { Java = 0, CPP=3, Python=8, Ruby=9 }; |
Let’s check the ways to convert enum to String in C++:
Using stringify macro method to convert enum to String in C++
It is a better way to convert an enum and it works in most of the cases. stringify()
macro can be used to convert text in the code into a string. In this method, no variable dereferencing or macro substitutions are not required.
Note: This method can convert only the exact text which is mentioned inside the parentheses.
For example:
1 2 3 4 5 |
int main(){ cout << stringify ( Java2Blog ) << endl; //Here all the leading and trailing whitespaces are ignored) } |
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
#include<iostream> #define stringify( name ) # name using namespace std; enum languages { java = 0, python, cpp, Ruby }; //converting enum elements to string with 'stringify()' const char* convert_enum[] = { stringify( java ), stringify( python ), stringify( cpp ), stringify( Ruby ) }; //function to print elements inside the enum void printelement( languages element ) { cout << convert_enum[ element ] << endl; } int main() { cout << ”Elements in the enum are: “ << endl; printelement((languages)0); printelement((languages)1); printelement((languages)2); printelement((languages)3); return 0; } </iostream> |
Output
java
python
cpp
Ruby
Using const char* Array to convert enum to String in C++
It is the simplest way to convert an enum. In this method we will use the default values of enum to access the elements in the string array. This method can also be called the most naive
method.
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> #include <string> using namespace std; enum languages { Java, Python, Ruby, CPP } ; static const char *enum_str[] = { "Java", "Python", "Ruby", "C++" }; int main(){ cout << enum_str[Java] << endl; cout << enum_str[Python] << endl; cout << enum_str[Ruby] << endl; cout << enum_str[CPP] << endl; return 0; } </string></iostream> |
Output
Python
Ruby
C++
Using a custom-defined function to convert enum to String in C++
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#include <iostream> #include <string> using namespace std; enum languages { Java, Python, Ruby, CPP } ; static const char *enum_str[] = { "Java", "Python", "Ruby", "C++" }; //custom-defined function string convert_enum (int value) { string str(enum_str[value]); return str; } int main(){ cout<< convert_enum(Java) << endl; cout<< convert_enum(Python) << endl; cout<< convert_enum(Ruby) << endl; cout<< convert_enum(CPP) << endl; return 0; } </string></iostream> |
Output
Python
Ruby
C++
Conclusion
We discussed different methods to convert enum to string. First method has the capability to handle big enums as well and other methods can be used for a simple conversion. Third party libraries can also be used to convert an enum to a string like this.
Happy Learning!!