简体   繁体   中英

how to search all map keys c++

What I am trying to implement is a basic spell checker which reads an input text file and compares the words to a small dictionary. If the word isn't in the dictionary then it is assumed to be spelled wrong, and then the user is prompted to spell the word correctly. This correctly spelled word is stored with its misspelled counterpart in a map. So far I have been able to do all this. What I cant figure out though is how to iterate over the entire map to check if that key (misspelled word) already exists without asking the user every time how to spell it if it is not found in that iteration of the map? Below is the code i have so far which checks to see if the word is in the dictionary, asks the user how to spell it if its not, and adding it to the map.

std::map<std::string,std::string>::iterator p = m_Replacements.begin();
            bool isIn = m_Dictionary.find(oneWord) != m_Dictionary.end();
            std::map<std::string,std::string>::iterator found = m_Replacements.find(oneWord);

            if(found==m_Replacements.end())
            {
                if( isIn == false)
                {
                    osInputPrompt<<"How do you correctly spell " + oneWord + "\n";
                    isInputPrompt >> correctSpell;
                    m_Replacements.insert(make_pair(textWord,correctSpell));
                    osOutput<<correctSpell + " ";
                }
                else
                {
                    osOutput<<oneWord + " ";
                }

So in summation, I would like to add another loop inside the loop that checks if the word is in the dictionary which would then check the whole map to see if the misspelled word is already there without asking "How do you correctly spell..." for each iteration of the map loop. Just for clarification I am not asking how to iterate over a map, I already know how to do that. Thanks in advance.

std::map<std::string,std::string>::iterator p;
std::string found;
for(p = m_repl.begin(); p != m_repl.end(); ++p)
{
     if(p->second == <what I am looking for>)
      {
         found = p->second;
         break;
     }
}

ie loop over the whole dictionary. More recent c++ version have nicer enumeration sytaxes - see How to use range-based for() loop with std::map?

got caught up in what i was doing but I figured it out by just adding a boolean like this to avoid asking the user for every iteration of the loop

bool mapFound;
                    for(std::map<std::string,std::string>::iterator it = m_Replacements.begin(); it!= m_Replacements.end(); it++)
                    {
                        std::string word = it->first;
                        if(word == oneWord)
                        {
                            mapFound = true;
                        }
                    }

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