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

I am doing some kind of word search in C ++, and although there is code for implementing the map, I want to make sure that it works using a map with keys and values ​​like std :: string, and using only keys as search queries without return values.

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 349900 words in the Dictionary.txt file and saves them on the map. Each line of the dictionary is just a search word; there is no definition or any value for communication. That's why I think that just keeping a pair of the same key and value on the map in order, and using find and first / second will also be ok? Please confirm.

+6
source share
1 answer

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

You should take a look at standard C ++ containers more. There is not much choice, and you should know them somehow (and choose or combine the right containers for the job)

+12
source

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


All Articles