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?
Qt
Example: the following numbers and lines are related to each other,
1: "Request System Info" 2: "Change System Info" 10: "Unkown Error"
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];
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).
Qt - QMap QHash.
QMap<int, QString> myMap; myMap[1234] = "Some value"; myMap[5678] = "Another value";
myMap.insert(1234, "Some value");
, .
, switch . C/++ , .
, 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; }
, ; .
struct Entry { unsigned int key; const char * text; };
; ( , ).
- :?
#include <sstream> using namespace std; string f(int i) { ostringstream oss; oss << i; return oss.str(); }
std::map
, , , 20 ( 20 , ). , . largestIndex - smallestIndex + 1. , , , - :
largestIndex - smallestIndex + 1
std::string GetStatus(int statusID){ return statusArray[statusID - smallestIndex]; }
statusArray - :
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, . , .
map
? ,
std:: ;
messages [key] = " "; messages [key2] = " ";
..
, -
std::ifstream ifile("the file") while (ifile && !ifile.eof()) { ifile >> key >> value; messages[key] = value; }
( ), .
, , std::pair<int, std::string> ( std::pair<int, char *>). , , , - , . : , , , 20 ints .
std::pair<int, std::string>
std::pair<int, char *>
: , std:: map, , . std:: map , , 20 20 , . node, , , , int, , , , - . , 20 .
Source: https://habr.com/ru/post/1725773/More articles:MFC GetClientRect ΠΈ MoveWindow Π½Π΅ ΡΠΎΡ ΡΠ°Π½ΡΡΡ ΡΠ°Π·ΠΌΠ΅Ρ? - visual-c++What type of file do you recommend to play a really small video? - c #csv utf-8 writer - python2.4 compatibility - pythonC # WPF - custom resizing - c #How to find out if data is readable from a socket in boost :: asio? - c ++Unable to put HTML element on top of flash memory - flashDisplay content via flash 9 swf? - safariProblem with WSDL - javaDetecting a map in a scene using OpenCV - opencvRuby + QT4; how to start - ruby ββ| fooobar.comAll Articles