如何对文本文件中的信息进行哈希处理
How to hash information from a text file?
我试图做的是从文本文件中读取一行,将其分解为构成它的单词,然后根据我不想散列的"坏单词"列表检查每个单词。每个不在坏词列表中的"好词"都应该被散列并在其索引中存储整行(如果这有意义的话)。因此,例如,"火环"将被拆分为"环"、"的"和"火"。我会散列"环"并存储"火环",我会看到"的"并注意到这是一个不好的词并跳过它,最后我会散列"火"并存储"火环"也用它。
我的代码按原样将一行分成单词,将其与坏词进行比较,并显示所有好词。然后,它关闭文件,重新打开它,并显示所有行。我在概念化时遇到的麻烦是如何将两者结合起来,同时对所有好词和整行进行哈希处理,以便我可以轻松存储它们。我应该怎么做?
#include <cstring>
#include <cctype>
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
const char * bad_words[] = {"of", "the", "a", "for", "to", "in", "it", "on", "and"};
ifstream file;
file.open("songs.txt");
//if(!file.is_open()) return;
char word[50];
while(file >> word)
{
// if word == bad word, dont hash
// else hash and store it in my hash table
bool badword = false;
for(int i = 0; i < 9; ++i)
{
if(strcmp(word, bad_words[i]) == 0)
{
badword = true;
}
}
if(badword) continue;
else
{
// get all words in a line that are not in bad_words
char * good_word = new char[strlen(word)+1];
strcpy(good_word, word);
cout << good_word << endl; // testing to see if works
// hash each good_word, store good_line in both of them
//int index = Hash(good_word);
//Add(good_line) @ table[index];
}
}
file.close();
file.open("songs.txt");
while(!file.eof()) // go through file, grab each whole line. store it under the hash of good_word (above)
{
char line[50];
file.getline(line, 50, 'n');
char * good_line = new char[strlen(line)+1];
strcpy(good_line, line);
cout << good_line << endl; // testing to see if works
}
return 0;
}
您似乎正在寻找std::unordered_multimap
.
我可能还会对一组"坏"词进行排序,并使用std::binary_search
来查看它是否包含特定单词。
std::vector<std::string> bad { "a", "and", "for" /* ... keep sorted */};
std::unordered_multimap<std::string, std::string> index;
while (std::getline(infile, line)) {
std::istringstream buf(line);
std::string word;
while (buf >> word)
if (!binary_search(bad.begin(), bad.end(), word))
index.insert(std::make_pair(word, line));
}
如果你真的必须实现自己的哈希表,你可以在这里找到哈希表数据结构的描述。
在最简单的形式中,哈希表是链表的数组。数组使用 hascode % arraySize 进行索引,链表负责哈希冲突。
相关文章:
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 使用Qt C++计算类似Git的SHA1哈希
- 如何将这个C++哈希表转换为动态扩展和收缩,而不是使用硬设置的最大值
- 用C++将哈希表写入文件并从文件中恢复
- c++找不到具有相同哈希的无序集合元素
- 哈希文件递归并保存到矢量Cryptopp中
- 对 pair<pair<int,int>pair<int,int unordered_map进行哈希处理>>
- 直接在 unordered_map 的方法中使用哈希,而不是生成哈希的用户定义对象
- 如何为字符串生成唯一但一致的 N 位哈希(小于 64 位)?
- 使用对象的基类部分模板专用化对对象进行哈希处理::哈希
- boost::包含提升单元的元组的哈希值
- 使用 Key 对 C++ 中的哈希映射进行排序. 无法排序
- C++中的并发哈希表
- 哈希映射使用 nullptr c++ 初始化节点的动态数组
- 如何将信息从 txt 文件添加到哈希表
- 如何计算没有标签信息的 mp3 文件的哈希值
- 通过UDP将信息哈希发送到Bittorrent Tracker
- 如何对文本文件中的信息进行哈希处理
- 将信息保存到哈希表或向量中