Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 109319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:03:02+00:00 2026-05-11T02:03:02+00:00

Requirements: Must be able to use C strings as well as C++ strings Fast

  • 0

Requirements:

  • Must be able to use C strings as well as C++ strings
  • Fast
  • No maps
  • No templates
  • No direct lookup, i.e. index might be out of bounds.
  • Index is not consecutive
  • Enums and strings contained in one header file
  • Only instantiate what you use.

This is what I have come up with so far:

- test.hh -  // Generic mapper //  // The idea here is to create a map between an integer and a string. // By including it inside a class we prevent every module which // includes this include file from creating their own instance. // struct Mapper_s {    int Idx;    const char *pStr; };  // Status enum State_t {    Running = 1,    Jumping = 6,     Singing = 12 };  struct State_s {    static const Mapper_s *GetpMap(void)    {        static Mapper_s Map[] =         {             { Running,   'Running' },             { Jumping,   'Jumping' },             { Singing,   'Singing' },             { 0,   0}         };         return Map;     }; };  - test.cc - // This is a generic function  const char *MapEnum2Str(int Idx, const Mapper_s *pMap) {     int i;     static const char UnknownStr[] = 'Unknown';      for (i = 0; pMap[i].pStr != 0; i++)     {         if (Idx == pMap[i].Idx)         {             return pMap[i].pStr;         }     }      return UnknownStr; }  int main() {    cout << 'State: ' << MapEnum2Str(State, State_s::GetpMap()) << endl;    return 0; } 

Any suggestions on how to improve this ?

I feel that the header file looks slightly cluttered…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T02:03:03+00:00Added an answer on May 11, 2026 at 2:03 am

    Here is what I settled on. Using this technique all you need to do is to include a header file. You will only instantiate what you use. You could also store a perfect hash table instead of just Idx & pStr. This approach does not work in C.

    file: e2str.hh

    struct Mapper_s {     int Idx;     const char *pStr; };  #define ENUM2STR_BEGIN(x) struct x { static const Mapper_s *GetpMap(void) {  static const Enum2StrMap_s Map[] =  #define ENUM2STR_END      return Map; }; }  const char *MapEnum2Str(int Idx, const Mapper_s *pMap); 

    file: mapper.hh

    #include 'e2str.hh'  ENUM2STR_BEGIN(State_s)  {         { Running,   'Running' },         { Singing,   'Singing' },         { Jumping,   'Jumping' },         { 0, 0} }; ENUM2STR_END; 

    file: test.cc

    #include 'mapper.hh' int main() {    cout << 'State: ' << MapEnum2Str(State, State_s::GetpMap()) << endl;    return 0; } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 65k
  • Answers 65k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer I think the ActiveRecord::Reflection class may be what you're looking… May 11, 2026 at 11:14 am
  • added an answer I did not have the SQLServer native client installed on… May 11, 2026 at 11:14 am
  • added an answer Just do something like this: Say you have table like… May 11, 2026 at 11:14 am

Related Questions

Requirements: Must be able to use C strings as well as C++ strings Fast
Here are the requirements: Must be alphanumeric, 8-10 characters so that it is user
How can I launch an application using C#? Requirements: Must work on Windows XP and
My requirements: Support .NET Compact Framework 2.0 and Windows Mobile 6.0 devices. Only need
Some of the requirements (restrictions) for such a ui framework/toolkit are: No single vendor
Premise : The requirements for an upcoming project include the fact that no one
Seems that requirements on safety do not seem to like systems that use AI
Given the extremely high requirements for unpredictability to prevent casinos from going bankrupt, what
If the requirements are changing frequently and you want to deliver your code in
How do you go about the requirements gathering phase? Does anyone have a good

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.