C++中的字典使用没有值的Map,只有键

Kth*_*ieu 10 c++ dictionary

我正在为c ++中的单词实现某种查找,虽然实现地图的代码在那里,但我想确保它是否能够使用带有键和值的映射作为std :: string,并且仅使用键作为没有返回值的查找.

    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)));
    }
Run Code Online (Sandbox Code Playgroud)

此代码接收Dictionary.txt文件中的349900个单词,并将它们存储在地图中.字典的每一行都是要查找的单词; 没有定义或任何关联的价值.这就是为什么我认为在地图中存储一对相同的键和值是可以的,并且使用find和first/second也可以.请确认.

Bas*_*tch 17

看起来你想要std :: set.它就像一张只有键很重要的地图,你从不关心或使用这个值.要查看在std::set<std::string>给定前缀之后表示为某个单词的字典,请考虑lower_bound

您应该更多地了解C++标准容器.没有那么多的选择,你应该以某种方式知道所有这些(并选择或组合适合工作的容器)