我可以在tr1无序地图中插入对象吗 我在C++中有大量的数据要处理,发现无序的映射是有效的。 但是,没有在无序映射中存储对象的示例。当我尝试自己的时候,它失败了

我可以在tr1无序地图中插入对象吗 我在C++中有大量的数据要处理,发现无序的映射是有效的。 但是,没有在无序映射中存储对象的示例。当我尝试自己的时候,它失败了,c++,hashmap,unordered-map,C++,Hashmap,Unordered Map,首先是无序的地图 #include <tr1/unordered_map> 编译器返回了一个大段错误,您能告诉我当我插入对象对时是否有问题,或者它不支持插入对象,如果不支持,是否有其他有效的方法来存储和搜索对象对,如JAVA中的hashmap。以下是complier中的错误消息报告: In file included from /usr/lib/gcc/x86_64-redhat-linux/4.4.5/../../../../include/c++/4.4.5/string:50

首先是无序的地图

#include <tr1/unordered_map>
编译器返回了一个大段错误,您能告诉我当我插入对象对时是否有问题,或者它不支持插入对象,如果不支持,是否有其他有效的方法来存储和搜索对象对,如JAVA中的hashmap。以下是complier中的错误消息报告:

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
类具有可访问的等式
运算符==
,并且定义了
std::hash

对于前者,您可能只需要使用一个成员函数:

struct Vector3f
{
  bool operator==(Vector3f const & rhs) const { /* ... */ }
  //...
};

严格地说,您只需要定义
std::equal_to
,但给类一个相等运算符是实现这一点的最明智的方法。

确保您的
Vector3f
类具有可访问的相等
运算符==
,并且定义了
std::hash

对于前者,您可能只需要使用一个成员函数:

struct Vector3f
{
  bool operator==(Vector3f const & rhs) const { /* ... */ }
  //...
};

严格地说,您只需要定义
std::equal_to
,但给类一个相等运算符是实现这一点最明智的方法

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提供哈希和谓词

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;
模板类无序映射;

您必须为自定义类型向量提供散列和谓词3f 对不起,我是C++中的新手,请告诉我如何在我的类中添加相等运算符。我将把这个类添加到question@user1069771:仅当发布类的当前定义时!:-)@user1069771:好的,简单地说
返回项目[0]==rhs.\u项目[0]&&&&u项目[1]==rhs.\u项目[1]&&u项目[2]==rhs.\u项目[2]顺便说一句,这个类是由我的讲师作为模板提供的,最好不要修改,因为它在模板中的其他位置使用。我想你这里的函数可能是向量3f和运算符=(Vector 3F和Obj[]):我仍然对此感到愤怒:什么样的自尊向量类不会与相等的比较来?我现在需要一些新鲜空气。对不起,我是C++中的新手,请告诉我如何在我的类中添加相等运算符。我将把这个类添加到question@user1069771:仅当发布类的当前定义时!:-)@user1069771:好的,简单地说
返回项目[0]==rhs.\u项目[0]&&&&u项目[1]==rhs.\u项目[1]&&u项目[2]==rhs.\u项目[2]顺便说一句,这个类是由我的讲师作为模板提供的,最好不要修改,因为它在模板中的其他位置使用。我认为你这里所指的函数可能是Vector3f&operator=(Vector3f&obj)[我仍然对此感到愤怒:什么样的自尊心向量类不会与相等比较一起出现?我现在需要一些新鲜空气。]
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;