Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C++ Data Types
  • C++ Input/Output
  • C++ Arrays
  • C++ Pointers
  • C++ OOPs
  • C++ STL
  • C++ Interview Questions
  • C++ Programs
  • C++ Cheatsheet
  • C++ MCQ
  • C++ Projects
  • C++ Exception Handling
  • C++ Memory Management
Open In App
Next Article:
How to Convert a std::string to char* in C++?
Next article icon

How to Convert an Enum to a String in C++?

Last Updated : 21 Feb, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In C, an enumeration is a user-defined data type whose members can be assigned some integer values. In this article, we are going to discuss how to convert an Enum to a String in C++.

Example:

Input:  myColor = GREEN;  Output:  Green

Convert an Enum to a String in C++

There are various methods to convert an enum to a string. A typical practice to convert an enum into a string is creating the mapping between values of enums and their string representations.

Approach

  • Define an enum that represents a collection of related constants, assigning each constant a meaningful name.
  • Use map to create a connection between enum values and their corresponding string representations.
  • Establish a clear association between each enum value and its corresponding string by populating the mapping data structure.
  • Develop a function that takes an enum value as input and returns its associated string by referencing the established mapping.
  • Return the corresponding string from the function based on the enum parameter.

C++ Program to Convert an Enum to a String

C++
// C++ Program to Convert an Enum to a String #include <iostream> #include <map> using namespace std;  // Define an enumeration named Color with constants RED, // GREEN, and BLUE enum Color { RED, GREEN, BLUE };  // Create a mapping from Color enum values to corresponding // strings map<Color, string> colorToString = { { RED, "Red" },                                      { GREEN, "Green" },                                      { BLUE, "Blue" } };  // Function to convert a Color enum value to its string // representation string enumToString(Color color) {     return colorToString[color]; } // Driver Code int main() {     // Declare a variable 'myColor' and assign it the value     // GREEN     Color myColor = GREEN;      // Display the string representation of the enum using     // the conversion function     cout << "Color: " << enumToString(myColor) << endl;      return 0; } 

Output
Color: Green  

Time Complexity: O(1)
Space Complexity: O(N), N is the number of items in the enum.




Next Article
How to Convert a std::string to char* in C++?
author
rajpootveerendrasingh36
Improve
Article Tags :
  • C++ Programs
  • C++
  • cpp-string
  • cpp-map
  • C++ Conversion Programs
  • CPP Examples
Practice Tags :
  • CPP

Similar Reads

  • How to Convert String to Date in C++?
    In C++, we generally store the date and time data in the form of the object of type std::tm. Sometimes, this data is stored inside a string object and we need to convert it into the tm type for further processing. In this article, we will learn how to convert a string to date in C++. Example: Input:
    2 min read
  • How to Convert Hex String to Byte Array in C++?
    A Hex String is a combination of the digits 0-9 and characters A-F and a byte array is an array used to store byte data types only. The default value of each element of the byte array is 0. In this article, we will learn how to convert a hex string to a byte array. Example: Input: Hex String : "2f4a
    2 min read
  • How to Convert a std::string to char* in C++?
    In C++, strings are the textual data that is represented in two ways: std::string which is an object, and char* is a pointer to a character. In this article, we will learn how to convert a std::string to char* in C++. Example Input:string myString = "GeeksforGeeks";Output:char * myString = "Geeksfor
    1 min read
  • How to Convert a Single Character to String in C++?
    A string is a generally made up of a sequence of multiple characters. In this article, we will learn how to convert a single character to a string in C++. Example: Input: c = 'A'Output: s = "a"Explanation: Character c is converted to a string. Input: c = '#'Output: s = "#"Explanation: Character c is
    4 min read
  • How to Convert Char Array to Int in C++
    In C++, a character array is treated as a sequence of characters also known as a string. Converting a character array into an integer is a common task that can be performed using various methods and in this article, we will learn how to convert char array to int in C++. Example Input:char arr1[] ="1
    2 min read
  • How to Convert a C++ String to Uppercase?
    Converting a string to uppercase means changing each character of the string that is in lowercase to an uppercase character. In the article, we will discuss how to convert a string to uppercase in C++. Examples Input: str = "Geeksforgeeks"Output: GEEKSFORGEEKSExplanation: Every character of string c
    3 min read
  • How to Convert Float to int in C++?
    In C++, the float is a short term for floating point value that is used to define numeric values with decimal points. The floating point numbers can be converted to integers using the process called type casting. In this article, we will learn how to convert a float value to an int in C++. Example:
    2 min read
  • Convert Float to String In C++
    In this article, we learn how we can convert float to string in C++ using different methods: Using the to_string()Using stringstreamUsing MacrosUsing lexical_cast from the boost library1. Using to_string() The to_string() method takes a single integer variable or other data type and converts it into
    3 min read
  • Convert String to Integer Vector in C++
    Strings are sometimes used to represent the numerical data which needs to be converted back to integer, but sometimes we may need to convert it to vector of integers instead due to some problems such as too large integer. In this article, we will learn different way to convert a string to integer ve
    2 min read
  • How to Convert std::string to Lower Case?
    Converting string to lower case means all the alphabets present in the string should be in lower case. In this article, we will learn how to convert the std::string to lowercase in C++. Examples Input: s = "GEEKS for GEEKS"Output: geeks for geeksExplanation: All characters of s are converted to lowe
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences