- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
编译时出现了很多错误:g++ -std=c++11 delme.cc
(源码取自另一个SO问题)
//delme.cc
#include<unordered_map>
#include <iostream>
#include <string.h>
#include <functional>
//using namespace std;
struct my_equal_to : public std::binary_function<char*, char*, bool> {
bool operator()(char* __x, char* __y)
{ return strcmp( __x, __y ) == 0; }
};
struct Hash_Func{
//BKDR hash algorithm
int operator()(char * str)const
{
int seed = 131;//31 131 1313 13131131313 etc//
int hash = 0;
while(*str)
{
hash = (hash * seed) + (*str);
str ++;
}
return hash & (0x7FFFFFFF);
}
};
//typedef unordered_map<char*, unsigned int, Hash_Func, my_equal_to> my_unordered_map;
int main(){
//my_unordered_map location_map;
std::unordered_map<char*, unsigned int, Hash_Func, my_equal_to> location_map;
//my_equal_to location_map;
char *p;
char a[10] = "ab"; p = a;
location_map.insert(std::pair<char*, unsigned int>(p, 10));
char b[10] = "abc"; p = b;
location_map.insert(std::pair<char*, unsigned int>(p, 20));
char c[10] = "abc"; p = c;
location_map.insert(std::pair<char*, unsigned int>(p, 20));
printf("map size: %d\n", location_map.size());
std::unordered_map<char*, unsigned int, Hash_Func, my_equal_to>::iterator it;
if ((it = location_map.find("abc")) != location_map.end())
{
printf("found!\n");
}
return 0;
}
错误信息太长,这里是其中的一部分
delme.cc: In function ‘int main()’:
delme.cc:44:49: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘std::_Hashtable<char*, std::pair<char* const, unsigned int>, std::allocator<std::pair<char* const, unsigned int> >, std::_Select1st<std::pair<char* const, unsigned int> >, my_equal_to, Hash_Func, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, true, false, true>::size_type {aka long unsigned int}’ [-Wformat]
delme.cc:46:38: warning: deprecated conversion from string constant to ‘std::_Hashtable<char*, std::pair<char* const, unsigned int>, std::allocator<std::pair<char* const, unsigned int> >, std::_Select1st<std::pair<char* const, unsigned int> >, my_equal_to, Hash_Func, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, true, false, true>::key_type {aka char*}’ [-Wwrite-strings]
In file included from /usr/include/c++/4.7/bits/hashtable.h:36:0,
from /usr/include/c++/4.7/unordered_map:46,
from delme.cc:1:
/usr/include/c++/4.7/bits/hashtable_policy.h: In instantiation of ‘static bool std::__detail::_Equal_helper<_Key, _Value, _ExtractKey, _Equal, _HashCodeType, true>::_S_equals(const _Equal&, const _ExtractKey&, const _Key&, _HashCodeType, std::__detail::_Hash_node<_Value, true>*) [with _Key = char*; _Value = std::pair<char* const, unsigned int>; _ExtractKey = std::_Select1st<std::pair<char* const, unsigned int> >; _Equal = my_equal_to; _HashCodeType = long unsigned int]’:
/usr/include/c++/4.7/bits/hashtable_policy.h:887:23: required from ‘bool std::__detail::_Hashtable_base<_Key, _Value, _ExtractKey, _Equal, _H1, _H2, _Hash, __cache_hash_code>::_M_equals(const _Key&, std::__detail::_Hashtable_base<_Key, _Value, _ExtractKey, _Equal, _H1, _H2, _Hash, __cache_hash_code>::_Hash_code_type, std::__detail::_Hash_node<_Value, __cache_hash_code>*) const [with _Key = char*; _Value = std::pair<char* const, unsigned int>; _ExtractKey = std::_Select1st<std::pair<char* const, unsigned int> >; _Equal = my_equal_to; _H1 = Hash_Func; _H2 = std::__detail::_Mod_range_hashing; _Hash = std::__detail::_Default_ranged_hash; bool __cache_hash_code = true; std::__detail::_Hashtable_base<_Key, _Value, _ExtractKey, _Equal, _H1, _H2, _Hash, __cache_hash_code>::_Hash_code_type = long unsigned int]’
...
有什么想法吗?
g++ (v.4.7.3) 在 Ubuntu 12.04 上运行。
更新 1
根据对这个问题的建议,这里是工作的。用于编译的命令是:g++ -std=c++11 -fpermissive delme.cc
。
#include<unordered_map>
#include <iostream>
#include <string.h>
#include <functional>
//using namespace std;
struct my_equal_to : public std::binary_function<char*, char*, bool> {
bool operator()(char* __x, char* __y)
{ return strcmp( __x, __y ) == 0; }
};
struct Hash_Func{
//BKDR hash algorithm
int operator()(char * str)const
{
int seed = 131;//31 131 1313 13131131313 etc//
int hash = 0;
while(*str)
{
hash = (hash * seed) + (*str);
str ++;
}
return hash & (0x7FFFFFFF);
}
};
//typedef unordered_map<char*, unsigned int, Hash_Func, my_equal_to> my_unordered_map;
char big[0x10001];
int main(int argc, char *argv[]){
//my_unordered_map location_map;
std::unordered_map<char*, unsigned int, Hash_Func, my_equal_to> location_map;
//my_equal_to location_map;
char cmd[100];
char *p;
int i;
for (i=0; i<0x10000; i++) big[i] = 'a';
char a[10] = "ab"; p = a;
for (i=0; i<0x100; i++) {
big[i] = 'b';
location_map[big] = i;
}
std::cout << "map size: " << location_map.size() << "\n";
//printf("map size: %d\n", location_map.size());
printf("result: %d\n", location_map[argv[1]]);
gets(cmd);
return 0;
}
最佳答案
delme.cc:44:49: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ...
这不是一个错误,它是一个警告,它声明 %d
是 int
类型,但是 unordered_map::size
不返回 int
。
在我看来,对这种打印使用类型安全操作会更好。例如。
// This works correctly regardless of the type returned by size(), assuming
// the type is supported by operator<<
std::cout << "map size: " << location_map.size() << "\n";
关于c++ - 编译涉及unordered_map的c++时出现很多错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29664449/
我目前正在寻找 std::map 的更好替代方案,并且遇到了帖子标题中提到的类。有人可以阐明它们之间的区别,不是在性能/API 方面,而是在它们与当前和 future 的通信标准相关的地方。 最佳答案
我正在尝试使用一个 unordered_map 和另一个 unordered_map 作为键(自定义哈希函数)。我还添加了一个自定义的 equal 函数,尽管它可能并不需要。 代码没有达到我的预期,但
关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。 这个问题是由于错别字或无法再重现的问题引起的。虽然类似的问题可能是on-topi
我正在尝试从 unordered_map 中返回 unordered_map 的拷贝。 下面的代码更清楚地说明了我的问题: typedef std::unordered_map Foo; typede
我有一个类型为unordered_map的容器,我想确认要向 map 添加元素时应使用哪个版本。我希望它使用新呈现的旧值覆盖旧的值(如果存在),如果不存在则仅添加它。 我看到insert会在元素退出时
所以我试图将 unordered_map 设置为另一个 unordered_map 的值。 现在我遇到了无法将值放入第二个 unordered_map 的问题。 我的代码看起来像这样。 std::
我有一个数据结构,它是 unordered_map 的 unordered_map: typedef std::unordered_map map1; typedef std::unordered_m
我们正在用 C++ 为学校开发一个游戏项目。我负责 map 对象,它将包含炸弹、玩家、墙壁和盒子等实体。我的 map 中有 3 个容器: 玩家的 std::list(多个玩家可以站在同一个盒子上)。
我正在使用 unordered_maps 的 unordered_map,这样我就可以使用“多键”语法来引用元素: my_map[k1][k2]。 有没有一种方便的方法可以在尝试访问之前使用相同的“多
假设我有一个 unordered_map 定义如下: unordered_map> f_table; f_table[1][3] = 10; f_table[1][2] = 1; f_table[1]
我正在 interviewbit.com 上解决竞争性编程问题我基本上使用 unordered_map 来跟踪访问过的数字。当我使用 operator[] 时,我的代码无法及时执行,但是当我使用 fi
我有一张 map ,如下所示。 struct B { int b1; int b2; int b3; }; struct A { B a1; B a2; }; unordered
我有以下数据结构问题?你能帮帮我吗?所以我的要求是在我将新数据项添加到此 map 时将此数据结构初始化为默认值。 我怎样才能有效地做到这一点? 对于我要添加的每个条目,我需要将 a1、a2、a3 设置
对于我的下一个任务,我需要使用一个非常大的散列;因为我有一个旧的编译器,所以我不能使用 C++0x std::unordered_map。理想情况下,我需要调用 reserve 为大量元素提前腾出空间
我不明白为什么这个简短示例中的第二个代码块无法正确编译。我的理解是 <> 中的第二个参数表示值,它不需要是唯一的。为什么第二个代码块抛出编译器错误,我需要做什么来补救它? // Unordered M
这段代码运行成功,结果为“Character Found”。 unordered_map mp; mp['a'] = 'b'; char b='b'; if(mp['a'] && mp['a'] ==
std::unordered_map::emplace和std::unordered_map::insert在C++中有什么区别? 最佳答案 unordered_map::insert 将键值对复制或
哪个更有效率?有什么好的基准吗? 最佳答案 C++11 的 std::unordered_map 规范类似于基于 tr1::unordered_map 的 boost::unordered_map。话
使用 gcc 4.8.1 和 libboost 1.53,根据我用于编译代码的优化级别,我得到了不同的结果。作为更大程序的一部分,函数 insertValues 对相同的 a、key 和 value
我正在尝试使用 boost::mulprecision 类型创建一个 STL(或 boost)unordered_map,例如cpp_int 但 gcc 在尝试将元素插入此容器后抛出错误。 #incl
我是一名优秀的程序员,十分优秀!