简体   繁体   中英

Find value on unordered_map

So in the beginning i had an unordered_map with an int as the key and string as the value initialized with 256 ASCII characters but later i found myself in the need to search for a string in the dictionnary but i couldnt use map::find since my key is an int . So here is my question how can i search for a specific value in an unordered map? or how should i modify this method to initialize all ASCII characters in a unordered_map where the key is a string and i could use map::find?

void InitDico (unordered_map<int,string>& Dico){
   Dico.clear();
   for (int i = 0; i < 256; ++i)
   {
       Dico[i] = (char)i;
   }}

What i tried :

void InitDico (unordered_map<string,int>& Dico){
   Dico.clear();
   for (int i = 0; i < 256; ++i)
   {
       Dico.insert({(char)i , i});  
   }}

std::string has a constructor that takes a count and a char , and constructs a string of count repetitions of that char .

It is implementation-defined whether a char object can hold negative values.

void InitDico2(std::unordered_map<std::string,int>& Dico) {
    Dico.clear();
    for (unsigned char c = 0; c < 256; c++)
    {
        Dico[{1, c}] = c;
    }    
}

So tanks to super i did it , i've done a really dumb mistake.... so here is the code :

void InitDico2(unordered_map<string,int>& Dico) {
Dico.clear();
string s ="";
char c;
for (int i = 0; i < 256; i++)
{
    c = (char)i;
    s += c;
    Dico[s] = i;
    s.clear();

}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM