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

  • How to initialize an array in Constructor in C++
    23 April

    How to initialize an Array in Constructor in C++

    Table of ContentsArraysConstructors of a ClassWays to initialize an array in Constructor in C++Initialize an array in Constructor With std::fill()Initialize an array in Constructor Using for LoopInitialize an array in Constructor Using Member Initializer ListConclusion This article discusses methods of initializing an array in constructor in C++. Let us first discuss arrays and constructors in […]

  • Write String to file in C++
    31 March

    Write String to File in C++

    Table of ContentsUsing the fstream Library to write String to File in C++Using the Insertion Operator (<<) With the fstream ObjectUsing the write() Function of the fstream LibraryUsing the fwrite() Function to write String to File in C++Conclusion The files are used to store the data permanently on a disk. This mitigates the problem of […]

  • Print Array in C++
    23 March

    Print Array in C++

    Table of ContentsIntroduction of ArraysDifferent Methods to Print an Array in C++print array in C++ by Traversing Indicesprint array in C++ Using for_each() Functionprint array in C++ Using Range-Based for Loopprint array in C++ Using Iteratorsprint array Using ostream_iterator() Function and copy() Functionprint array Using C++17 copy() Function and make_ostream_joiner() FunctionConclusion An array is a […]

  • Print Vector C++
    23 March

    Print Vector in C++

    Table of ContentsPrint Vector in C++ Using IndicesPrint Vector in C++ Using Range-Based for LoopPrint Vector in C++ Using for_each() FunctionPrint Vector in C++ Using IteratorPrint Vector in C++ by Overloading the Insertion Operator (<<)Print Vector in C++ Using copy() FunctionConclusion This article discusses the vector and how to print vector in C++. Vectors are […]

  • Print Map in C++
    15 March

    Print Map in C++

    Table of ContentsMap vs Unordered MapPrint Map in C++ Using IteratorPrint Map in C++ Using Range-Based for LoopPrint Map in C++ by Overloading the ostream Insertion Operator (<<)Print Map in C++17 Using Structure Binding and Range-Based for LoopPrint Map in C++17 Using for_each() FunctionConclusionReferences Maps in C++ store the data in form of key and […]

  • How to write binary files in C++
    14 March

    Write Binary Files in C++

    Table of ContentsOpening a File in Binary Mode Using fstream LibraryWriting the Binary File Using the write() Function of fstream LibraryWriting the Binary File Using the put() Function of fstream LibraryWriting the Binary File Using Insertion Operator (<<) Of fstream LibraryWriting the Structure Object to Binary File Using fstream LibraryConclusion In this post, we will […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.