我可以插入对象到tr1 unordered_map
Can I insert objects into tr1 unordered_map
我有大量的数据需要在c++中处理,并且发现unordered_map工作效率很高但是,没有在unorderd_map中存储对象的示例。当我自己尝试时,它无法完成。
首先包含unordered_map
#include <tr1/unordered_map>
然后定义类型
typedef std::tr1::unordered_map<Vector3f, Vector3f> vectorMap;
Vector3f是我创建的类。当我在上面编译时,它没有返回任何错误。但是当我添加
vectorMap _map;
Vector3f v1,v2,v3;
_map.insert(std::make_pair(v1,v2));
编译器返回一大段错误,请告诉我插入对时是否有问题,或者它unsorted不支持插入对象,如果不是,在JAVA中是否有其他有效的方式存储和搜索对象对,如hashmap。以下是编译器中报告的错误信息:
In file included from /usr/lib/gcc/x86_64-redhat-linux/4.4.5/../../../../include/c++/4.4.5/string:50,
from /usr/lib/gcc/x86_64-redhat-linux/4.4.5/../../../../include/c++/4.4.5/bits/locale_classes.h:42,
from /usr/lib/gcc/x86_64-redhat-linux/4.4.5/../../../../include/c++/4.4.5/bits/ios_base.h:43,
from /usr/lib/gcc/x86_64-redhat-linux/4.4.5/../../../../include/c++/4.4.5/ios:43,
from /usr/lib/gcc/x86_64-redhat-linux/4.4.5/../../../../include/c++/4.4.5/ostream:40,
from /usr/lib/gcc/x86_64-redhat-linux/4.4.5/../../../../include/c++/4.4.5/iostream:40,
from homework1.cc:1:
/usr/lib/gcc/x86_64-redhat-linux/4.4.5/../../../../include/c++/4.4.5/bits/stl_function.h: In member function ‘bool std::equal_to<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = Vector3f]’:
/usr/lib/gcc/x86_64-redhat-linux/4.4.5/../../../../include/c++/4.4.5/tr1_impl/hashtable_policy.h:769: instantiated from ‘bool std::tr1::__detail::_Hash_code_base<_Key, _Value, _ExtractKey, _Equal, _H1, _H2, std::tr1::__detail::_Default_ranged_hash, false>::_M_compare(const _Key&, size_t, std::tr1::__detail::_Hash_node<_Value, false>*) const [with _Key = Vector3f, _Value = std::pair<const Vector3f, Vector3f>, _ExtractKey = std::_Select1st<std::pair<const Vector3f, Vector3f> >, _Equal = std::equal_to<Vector3f>, _H1 = std::tr1::hash<Vector3f>, _H2 = std::tr1::__detail::_Mod_range_hashing]’
/usr/lib/gcc/x86_64-redhat-linux/4.4.5/../../../../include/c++/4.4.5/tr1_impl/hashtable:918: instantiated from ‘std::tr1::__detail::_Hash_node<_Value, __cache_hash_code>* std::tr1::_Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, __cache_hash_code, __constant_iterators, __unique_keys>::_M_find_node(std::tr1::__detail::_Hash_node<_Value, __cache_hash_code>*, const _Key&, typename std::tr1::_Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, __cache_hash_code, __constant_iterators, __unique_keys>::_Hash_code_type) const [with _Key = Vector3f, _Value = std::pair<const Vector3f, Vector3f>, _Allocator = std::allocator<std::pair<const Vector3f, Vector3f> >, _ExtractKey = std::_Select1st<std::pair<const Vector3f, Vector3f> >, _Equal = std::equal_to<Vector3f>, _H1 = std::tr1::hash<Vector3f>, _H2 = std::tr1::__detail::_Mod_range_hashing, _Hash = std::tr1::__detail::_Default_ranged_hash, _RehashPolicy = std::tr1::__detail::_Prime_rehash_policy, bool __cache_hash_code = false, bool __constant_iterators = false, bool __unique_keys = true]’
/usr/lib/gcc/x86_64-redhat-linux/4.4.5/../../../../include/c++/4.4.5/tr1_impl/hashtable:983: instantiated from ‘std::pair<typename std::tr1::_Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, __cache_hash_code, __constant_iterators, __unique_keys>::iterator, bool> std::tr1::_Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, __cache_hash_code, __constant_iterators, __unique_keys>::_M_insert(const _Value&, std::tr1::true_type) [with _Key = Vector3f, _Value = std::pair<const Vector3f, Vector3f>, _Allocator = std::allocator<std::pair<const Vector3f, Vector3f> >, _ExtractKey = std::_Select1st<std::pair<const Vector3f, Vector3f> >, _Equal = std::equal_to<Vector3f>, _H1 = std::tr1::hash<Vector3f>, _H2 = std::tr1::__detail::_Mod_range_hashing, _Hash = std::tr1::__detail::_Default_ranged_hash, _RehashPolicy = std::tr1::__detail::_Prime_rehash_policy, bool __cache_hash_code = false, bool __constant_iterators = false, bool __unique_keys = true]’
/usr/lib/gcc/x86_64-redhat-linux/4.4.5/../../../../include/c++/4.4.5/tr1_impl/hashtable:420: instantiated from ‘typename __gnu_cxx::__conditional_type<__unique_keys, std::pair<std::tr1::__detail::_Hashtable_iterator<_Value, __constant_iterators, __cache_hash_code>, bool>, std::tr1::__detail::_Hashtable_iterator<_Value, __constant_iterators, __cache_hash_code> >::__type std::tr1::_Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, __cache_hash_code, __constant_iterators, __unique_keys>::insert(const _Value&) [with _Key = Vector3f, _Value = std::pair<const Vector3f, Vector3f>, _Allocator = std::allocator<std::pair<const Vector3f, Vector3f> >, _ExtractKey = std::_Select1st<std::pair<const Vector3f, Vector3f> >, _Equal = std::equal_to<Vector3f>, _H1 = std::tr1::hash<Vector3f>, _H2 = std::tr1::__detail::_Mod_range_hashing, _Hash = std::tr1::__detail::_Default_ranged_hash, _RehashPolicy = std::tr1::__detail::_Prime_rehash_policy, bool __cache_hash_code = false, bool __constant_iterators = false, bool __unique_keys = true]’
homework1.cc:181: instantiated from here
/usr/lib/gcc/x86_64-redhat-linux/4.4.5/../../../../include/c++/4.4.5/bits/stl_function.h:203: error: no match for ‘operator==’ in ‘__x == __y’
这是Vector3f类
class Vector3f {
float _item[3];
public:
float & operator [] (int i) {
return _item[i];
}
Vector3f(float x, float y, float z)
{ _item[0] = x ; _item[1] = y ; _item[2] = z; };
Vector3f() {};
Vector3f & operator = (Vector3f & obj)
{
_item[0] = obj[0];
_item[1] = obj[1];
_item[2] = obj[2];
return *this;
};
Vector3f & operator += (Vector3f & obj)
{
_item[0] += obj[0];
_item[1] += obj[1];
_item[2] += obj[2];
return *this;
};
};
确保您的Vector3f
类具有可访问的等式operator==
,并且std::hash<Vector3f>
已定义。
对于前者,您可能应该使用成员函数:
struct Vector3f
{
bool operator==(Vector3f const & rhs) const { /* ... */ }
//...
};
严格地说,您所需要的只是定义了std::equal_to<Vector3f>
,但是给您的类一个相等操作符是实现这一目标的最明智的方法。
unorderd_map声明如下
template<class Key,
class T,
class Hash = hash<Key>,
class Pred = std::equal_to<Key>,
class Alloc = std::allocator<std::pair<const Key, T> > > class unordered_map;
您必须为您的自定义类型Vector3f
相关文章:
- 如何导出包含具有"std::unique_ptr"值的"std::map"属性的
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- 使用一个考虑到std::map中键值的滚动或换行的键
- 为什么 const std::p air<K,V>& 在 std::map 上基于范围的 for 循环不起作用?
- 允许从 std::map 的密钥窃取资源?
- 有没有办法对std::unordered_set、std::unrdered_map、std::set、std::map
- 将重物插入std::map
- 使用通用值初始化 std::map,不重复
- 仅包含可移动 std::map 的类的移动构造函数不起作用
- C++:当所有条目都保证是唯一时,替代 std::map
- 使用模板化的键类型定义 std::map,该键类型基于作为参数接收的函数
- 如果 KEY 是 std::list 或 std::vector 而不是值,那么 std::map 的默认行为是什么?
- 使用字符数组作为 Map 中的键
- C++如何创建 std::map
- C++ equivalent to Java Map getOrDefault?
- 从其他容器中移动构造"std::map"
- 如何使用 uint64_t 键类型从 std::map<int, std::string> 返回值?
- 将 std::map::emplace 与返回 shared_ptr 的函数一起使用是否正确?
- C++中 std::map 的运行时复杂度是多少?
- unordered map -在c++ std::unordered_map中预分配桶