How can I match an int to the corresponding string in C / C ++

I have 20 digits and I would like to associate them with strings. Is there a faster way besides using the case switch statement to achieve this.

I need to convert int to the appropriate string, and the numbers will not necessarily be packed. Could some code be useful in Qt?

Example: the following numbers and lines are related to each other,

1:   "Request System Info"

2:   "Change System Info"

10:  "Unkown Error"
+3
source share
10 answers

easier way to use the card

std::map<int, std::string> mymap;
mymap[1] = "foo";
mymap[10] = "bar";
// ...
int idx = 10;
std::string lookup = mymap[idx];
+4
source

I recommend std :: map <>

#include <map>
#include <string>

std::map<int, std::string> mapping;

// Initialize the map
mapping.insert(std::make_pair(1, "Request System Info"));
mapping.insert(std::make_pair(2, "Change System Info"));
mapping.insert(std::make_pair(10, "Unkown Error"));

// Use the map
std::map<int, std::string>::const_iterator iter =
    mapping.find(num);
if (iter != mapping.end())
{
    // iter->second contains your string
    // iter->first contains the number you just looked up
}

, initalizer- ++ 0x, :

std::map<int, std::string> mapping = {{1, "Request System Info"},
                                      {2, "Change System Info"}
                                      {10, "Unkown Error"}};

std:: map < > , std:: map < > :: find O (log N). - ++ 0x, std:: unordered_map < > O (1).

+17

Qt - QMap QHash.

QMap<int, QString> myMap;
myMap[1234] = "Some value";
myMap[5678] = "Another value";

myMap.insert(1234, "Some value");

, .

+7

, switch . C/++ , .

+3

, C:

#include <stdio.h>

struct message {
    int val;
    const char *msg;
};

int main(void)
{
    struct message messages[] = {
        {1, "Request System Info"},
        {2, "Change System Info"},
        {10, "Unkown Error"}
    };
    size_t nmessages = sizeof messages / sizeof messages[0];
    size_t i;

    for (i=0; i < nmessages; ++i) {
        printf("%d : '%s'\n", messages[i].val, messages[i].msg);
    }
    return 0;
}
+3

, ; .

struct Entry
{
   unsigned int  key;
   const char *  text;
};

; ( , ).

+2

- :?

#include <sstream>
using namespace std;
string f(int i)
{
    ostringstream oss;
    oss << i;
    return oss.str();
}
+1

std::map


, , , 20 ( 20 , ). , . largestIndex - smallestIndex + 1. , , , - :

std::string GetStatus(int statusID){
    return statusArray[statusID - smallestIndex];
}

statusArray - :

void SetStatus(int statusID, std::string description){
    statusArray[statusID - smallestIndex] = description;
}

void InitStatuses(){
    statusArray = new std::string[largestIndex - smallestIndex + 1];
    SetStatus(1, "Request System Info");
    SetStatus(2, "Change System Info");
    SetStatus(10, "Unknown Error");
}

, map, . , .

0

? ,

std:: ;

messages [key] = " "; messages [key2] = " ";

..

, -

std::ifstream ifile("the file")

while (ifile && !ifile.eof())
{
  ifile >> key >> value;
  messages[key] = value;
}

..

0

( ), .

, , std::pair<int, std::string> ( std::pair<int, char *>). , , , - , . : , , , 20 ints .

: , std:: map, , . std:: map , , 20 20 , . node, , , , int, , , , - . , 20 .

0

Source: https://habr.com/ru/post/1725773/


All Articles