Home > Backend Development > C++ > How Can I Print the Textual Representation of a C Enum Value?

How Can I Print the Textual Representation of a C Enum Value?

Mary-Kate Olsen
Release: 2024-11-30 08:28:11
Original
994 people have browsed it

How Can I Print the Textual Representation of a C   Enum Value?

C : Printing Enum Value as Text

Enum types provide a convenient way to represent a set of constants with symbolic names. By default, enums are represented as integer values. However, in situations where you need to display the actual text associated with an enum value, the default behavior may not be suitable.

Let's consider an example:

enum Errors {
    ErrorA = 0,
    ErrorB,
    ErrorC,
};

Errors anError = ErrorA;
std::cout << anError; // Outputs "0"
Copy after login

In this example, we have an Errors enum with three possible values: ErrorA, ErrorB, and ErrorC, where ErrorA has a numerical value of 0. When we attempt to print the anError variable, it outputs "0" instead of the desired "ErrorA".

To solve this issue without resorting to if/switch statements, several methods can be employed:

1. Using a Map:

#include <map>
#include <string_view>

// Define a map to associate enum values with their string representations
std::map<Errors, std::string_view> errorStrings = {
    {ErrorA, "ErrorA"},
    {ErrorB, "ErrorB"},
    {ErrorC, "ErrorC"},
};

// Overload the `<<` operator to print the string representation
std::ostream& operator<<(std::ostream& out, const Errors value) {
    out << errorStrings[value];
    return out;
}
Copy after login

With this method, the overloaded << operator looks up the string representation associated with an enum value in the errorStrings map and outputs it.

2. Using an Array of Structures:

#include <string_view>

// Define a struct to store enum values and string representations
struct MapEntry {
    Errors value;
    std::string_view str;
};

// Define an array of structures
MapEntry entries[] = {
    {ErrorA, "ErrorA"},
    {ErrorB, "ErrorB"},
    {ErrorC, "ErrorC"},
};

// Overload the `<<` operator to perform a linear search and print the string representation
std::ostream& operator<<(std::ostream& out, const Errors value) {
    for (const MapEntry& entry : entries) {
        if (entry.value == value) {
            out << entry.str;
            break;
        }
    }
    return out;
}
Copy after login

This method uses an array of structures to store the enum values and their string representations. A linear search is performed to find the matching string for the specified enum value.

3. Using switch/case:

#include <string>

// Overload the `<<` operator to print the string representation using `switch/case`
std::ostream& operator<<(std::ostream& out, const Errors value) {
    switch (value) {
        case ErrorA: out << "ErrorA"; break;
        case ErrorB: out << "ErrorB"; break;
        case ErrorC: out << "ErrorC"; break;
    }
    return out;
}
Copy after login

In this method, the overloaded << operator uses a switch/case statement to directly print the appropriate string representation for the given enum value.

The above is the detailed content of How Can I Print the Textual Representation of a C Enum Value?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template