簡體   English   中英

我可以將對象插入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));

complier返回一大段錯誤,你能告訴我插入對時有什么問題嗎,或者unorted不支持插入對象,如果沒有,是否有任何其他有效的方法存儲和搜索像hashmap這樣的對象JAVA。 以下是編譯器中的錯誤消息報告:

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提供哈希和謂詞

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM