Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dictionary in C++ using a Map with no values, only keys

Tags:

c++

dictionary

I'm implementing some sort of lookup for words in c++, and while the code for implementing a map is there, I wanna make sure if it works that using a map with keys and values as std::string, and using only keys as lookups without a value to return.

    std::vector< std::string> DictionLines;
    Reader DictionReader(Dictionary);
    DictionLines = DictionReader.getLines();
    std::map<std::string, std::string> DictionaryM;

    for (int t = 0; t < DictionLines.size(); ++t) {
        DictionaryM.insert(std::pair<std::string, std::string>(DictionLines.at(t), DictionLines.at(t)));
    }

This code takes in the 349900 words in a Dictionary.txt file, and stores them in the map. Each line of the dictionary is just the word to lookup; no definition or any value to associate. Which is why I think just storing a pair of the same key and value in the map is ok, and using find and first/second would also be fine? Please confirm.

like image 349
Kthieu Avatar asked Mar 14 '15 06:03

Kthieu


1 Answers

It looks like you want std::set. It is like a map where only keys matter and you never care or use the value. To look in a dictionary represented as a std::set<std::string> for some word after a given prefix, consider lower_bound

You should look more into C++ standard containers. There are not that much choice, and you should somehow know all of them (and choose or combine the right containers for the job)

like image 158
Basile Starynkevitch Avatar answered Sep 28 '22 13:09

Basile Starynkevitch