Convert enum to string in C++

Enum to String in C++

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:

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:

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:

Code

Output

Elements in the enum are:
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

Output

Java
Python
Ruby
C++

Using a custom-defined function to convert enum to String in C++

Code

Output

Java
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!!

Related Posts

  • Read CSV file in C++
    01 November

    How to read CSV file in C++?

    Table of ContentsWhat is a CSV File? How To Read A CSV File In C++?C++ Code To Read A CSV FileHow To Read A CSV File Using C-Based Approach?Conclusion CSV files or Comma Separated Value Files are simple yet special files that we use to store data. Here, each value is separated by a comma, and […]

  • Count lines in file in C++
    01 November

    Count lines in file in C++

    Table of ContentsHow To Count Lines In A File In C++?C++ Program To Count Lines In A FileHow To Count Lines In A File In C++ Using C-style Approach?C++ Program to Count Lines In A File using C-style approachConclusion We often store our data into files when we need the data to be persistent and […]

  • Get Number of Elements in Array in C++
    08 October

    Get Number of Elements in Array in C++

    Table of ContentsGet Number of Elements in Array in C++Using the sizeof() functionUsing the pointersUsing the size() functionGet the frequency of each element in array in C++Using nested for loopUsing maps One of the most fundamental collections in any programming language is an array. It stores a similar type of data at specific indices that […]

  • Trim String in C++
    19 July

    Trim String in C++

    Table of ContentsUsing Boost string algorithms to trim string in C++Using find_first_not_of() and find_last_not_of()` to trim strings in C++Using find_if() to trim the string in C++Using stringstream to trim the strings in C++Using a customized function to trim the strings in C++Conclusion When we take an input from an user, strings can have unwanted whitespaces […]

  • JSON parser in C++
    19 July

    JSON Parser in C++

    Table of ContentsSimple JSON Parser in C++ using JsonCpp libraryConclusion In this post, we will see about JSON parser in C++. There is no native support for JSON in C++. We can use a number of libraries that provide support for JSON in C++. We will use JsonCpp to parse JSON files in C++ which […]

  • C++ random number between 0 and 1
    11 June

    C++ random number between 0 and 1

    Table of ContentsUsing the rand() function to generate random number between 0 and 1 in C++Using the std::uniform_real_distribution() function to generate random number between 0 and 1 in C++ Random numbers are frequently used in programming for test case generation, cryptography, and other related purposes. In this tutorial, we will generate random number between 0 […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.