簡體   English   中英

std :: list找不到類型

[英]std::list cannot find type

我寫了自己的Hashmap實現。 作為基本類型,我使用

template <class K, class V>
class HashMap{
public:
    HashMap(){ table.reserve(100); }

    bool insert(const K& key, const V& value) {
        size_t index_pos = find_hash_position(key);
        table[index_pos].push_back(std::make_pair(key, value));
        return true;
    }
private:
    std::vector<std::list<std::pair<K, V>>> table;
};

然后,我嘗試創建Hashmap並將值插入:

ConcurrentHashMap<std::pair<int, int>, int)> hash_table;
hash_table.insert(std::make_pair(1, 1), 2);

但是我有一個139錯誤,

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff768a58f in std::__detail::_List_node_base::_M_hook(std::__detail::_List_node_base*) () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
Python Exception <type 'exceptions.ValueError'> Cannot find type std::__cxx11::list<std::pair<std::pair<int, int>, int>, std::allocator<std::pair<std::pair<int, int>, int> > >::iterator::_Node: 
Python Exception <type 'exceptions.ValueError'> Cannot find type std::__cxx11::list<std::pair<std::pair<int, int>, int>, std::allocator<std::pair<std::pair<int, int>, int> > >::iterator::_Node: 

我錯了什么以及如何克服這個問題?

您的代碼出現分段錯誤,因為向量的大小為零。 std :: vector最初不分配任何空間,每當您向其中推送內容時,它們的空間都會擴大。

因此,您應該檢查“ index_pos”,如果索引大於向量容量,請調整向量的大小。

bool insert(const K& key, const V& value) {
    size_t index_pos = find_hash_position(key);
    if(index_pos >= table.size())
    {
        table.resize(index_pos+1);
    }
    table[index_pos].push_back(std::make_pair(key, value));
    return true;
}

暫無
暫無

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

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