简体   繁体   English

C++ 中的散列词?

[英]Hashing words in C++?

I have a text file that I read the data from and search the names inside to keep track of it.我有一个文本文件,我从中读取数据并搜索其中的名称以跟踪它。 I want to use Hashing instead of Arrays for the speed of search, and I don't want to insert a name twice if it's already included in the hash.我想使用散列而不是 Arrays 来提高搜索速度,如果名称已经包含在 hash 中,我不想插入两次。

(I found some code about hashing but the example code was for numbers not for strings or words. How should I approach? Keep the first letter in ASCII or combine all letters and % by a number? Not sure exactyle how to do it.) (我找到了一些关于散列的代码,但示例代码用于数字而不是字符串或单词。我应该如何处理?将第一个字母保留在 ASCII 中或将所有字母和 % 组合成一个数字?不确定如何做。)

Can you provide a short sample code if it's possible?如果可能的话,你能提供一个简短的示例代码吗? Let's say;比方说; get every word in a text file with Getline and add it to Hash Table if the word is not included already.使用 Getline 获取文本文件中的每个单词并将其添加到 Hash 表中(如果该单词尚未包含)。

Method does not matter (Chaining, linear probing etc.)方法无关紧要(链接、线性探测等)

Please do not use any fancy library.请不要使用任何花哨的库。

You can just use an unordered_set您可以只使用unordered_set

#include <iostream>
#include <string>
#include <unordered_set>
#include <fstream>

std::unordered_set<std::string> file_to_unordered_set(const std::string& filename) {
    std::unordered_set<std::string> tbl;
    std::ifstream fs(filename);
    if (!fs) {
        throw std::runtime_error("bad file");
    }

    std::string line;
    while (std::getline(fs, line)) {
        tbl.insert(line);
    }
    return tbl;
}

int main() {
    auto words = file_to_unordered_set("<some file path>");
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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