- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试读取单词列表并将它们连同它们在按字母顺序排序的文件中的位置一起保存在 C++ STL hash_map 中。这个想法是稍后我需要能够判断一个字符串是否是一个词以及它是在另一个词之前还是之后。
ifstream f_dict ("dictionary.txt");
__gnu_cxx::hash_map <const char*, int> dictionary;
string temp_str;
int counter = 0;
while (!f_dict.eof()) {
f_dict >> temp_str;
dictionary.insert(make_pair(temp_str.c_str(), counter++));
}
我遇到的问题是它没有保存实际的单词。下面的 for 循环
打印出选择的单词,但 iter->first
始终为空。我错过了什么?
__gnu_cxx::hash_map<const char*, int>::iterator iter;
int i = 0;
for (iter = dictionary.begin(); iter != dictionary.end() && i < 150; iter++) {
cout << "word: " << iter->first << " index: " << iter->second << "\n";
i++;
}
最佳答案
您正在尝试为每个单词存储相同的 const char *,因为您从未为从文件中提取的单词创建任何新内存。如果您打印出从 temp_str.c_str()
返回的指针,它对于您的第一个循环中的每个调用都是相同的。在你的第二个循环中,你为 map 中的每条记录打印出相同的 char * (注意只有 1 个 b/c map 不允许重复),它在第一个循环内或在第一个循环和之间被设置为空字符串你的 for 循环。
这是演示问题和解决方案的示例代码。
#include <fstream>
#include <iostream>
#include <map>
using namespace std;
int main (int argc, char **argv)
{
ifstream file("test.txt");
map<const char *, int> dictionary;
map<string, int> strDictionary;
string temp_str;
int counter = 0;
while (!file.eof())
{
file >> temp_str;
cout << "PARSED: " << temp_str << "\n";
cout << "INSERTING: " << (unsigned long) temp_str.c_str() << "\n";
dictionary.insert(make_pair(temp_str.c_str(), counter));
strDictionary.insert(make_pair(temp_str, counter));
counter++;
}
cout << "Dictionary Size: " << dictionary.size() << "\n";
cout << "Str Dictionary Size: " << strDictionary.size() << "\n";
for (map<const char*, int>::const_iterator iter = dictionary.begin();
iter != dictionary.end();
++iter)
{
cout << "CHAR * DICTINARY: " << iter->first << " -> " << iter->second << "\n";
}
for (map<string, int>::const_iterator iter = strDictionary.begin();
iter != strDictionary.end();
++iter)
{
cout << "STR DICTIONARY: " << iter->first << " -> " << iter->second << "\n";
}
return 1;
}
关于C++ 将文件读入 hash_map,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1535249/
在visual C++下,我们有“hash_map”和“hash_set”。在 g++ 中,我们有“stdext::hash_map”和“stdext::hash_set”。它们在各自的性能或其他因素
我在 Ubuntu 中使用 GCC 定义了一个 hash_map stringHashMap。我保证 stringHashFunction 是正确的,因为我可以在 hash_map 中正确使用 str
我知道这应该很容易,但我就是没能消除编译错误。这是我的代码: template struct basic_field_type_map : public hash_map, basic_string>
我正在尝试读取单词列表并将它们连同它们在按字母顺序排序的文件中的位置一起保存在 C++ STL hash_map 中。这个想法是稍后我需要能够判断一个字符串是否是一个词以及它是在另一个词之前还是之后。
#include using namespace std; class hash_t : public __gnu_cxx::hash_map > { }; hash_t hash; ... 我在使
我想要一个 std::hash_map 将(例如)常规 std:string 映射到 多个不同的特化另一个模板类。 这个例子是我想要实现的(虽然它是错误的并且无法编译): template class
hash_map 和 hash_set header 尚未包含在 C++ 标准中,但它们可作为我最近使用的所有编译器的扩展使用。 我想知道在不牺牲可移植性的情况下,在实际代码中我可以在多大程度上依赖这
我想看一个如何正确重写 stdext::hash_compare 的简单示例,以便为我自己的用户定义类型定义新的哈希函数和比较运算符。我正在使用 Visual C++ (2008)。 最佳答案 这就是
我使用以下结构: hash_map> 当我最初用从文本文件中读取的信息填充散列映射时,我可以毫无问题地将元素插入到那些 time_t 列表中。 hash_t::iterator it = hash.f
是否保证当 hash_map/unordered_map 加载相同的项目时,它们在迭代时具有相同的顺序?基本上我有一个 HashMap ,我从一个文件加载它,我定期将有限数量的项目提供给一个例程,之后
我有一个 HashMap 定义为 class KeyType { int key; mutable bool flag; KeyType(int key) : key(key)
我是 c++ 中的 hash_map 的新手。我必须将表转换为散列图。 这就是我在程序中声明和使用 hash_map 的方式 我正在使用 Microsoft Visual Studio。 #inclu
如何将迭代器作为 hash_map 的键? 你会如何在 gcc、Microsoft c++ 下定义它? 例如 vector::iterator i; hash_map::iterato
我有一个多线程应用程序,它使用 C++ STL hash_map 来存储键值对。 hash_map 定义如下: struct eqstr { bool operator()(const strin
我正在比较我编写的一个简单的哈希函数,该函数只是将它乘以一个质数模另一个质数(表大小),结果证明 STL 慢了 100 倍。这是我写的测试方法: stdext:: hash_map hashDict;
我创建了一个小类并尝试将其用作 STL hash_map 中的数据。大量搜索让我知道我需要添加一些东西作为分配器,但到目前为止我还没有看到如何在我的类里面使用 STL 中的默认分配器的示例。有人可以帮
key 的哈希值被计算出来并除以一个质数。一般来说,是否有任何标准素数(比如 32/64 位)? 我的理解是哈希表不可调整大小/不可调整,其内部数组取决于此。如果我有一个只有 5 个元素的哈希表,键空
我想访问 C++ hash_map 的散列值。我试过: __gnu_cxx::hash_map my_table; const hash hh = my_table.hash_funct(); str
我正在开发一个高性能应用程序,其中所有调用都必须合理。我有一张 map ,在每次交易开始时使用一次来进行我想改进的查找。 map 在启动时加载,之后不会改变。 下面映射中的键是一个 std::stri
使用 STL C++ hash_map... class MyKeyObject { std::string str1; std::string str2; bool oper
我是一名优秀的程序员,十分优秀!