使用对数据类型向量的哈希表中的分段错误错误

时间:2019-07-11 13:04:29

标签: c++ c++11

我正在尝试使用成对数据类型的向量来实现哈希表。我想我已经实现了哈希表的原理工作而没有任何编译错误,但是当我尝试运行时,它抛出了分段错误错误。

包括

 #include <stdexcept>

 #include <string>

 #include <vector>

 using namespace std;

 template < class T >
  class HTable {
    private:
        vector < pair < string, T >> data;
    vector < bool > positions_in_use;
    size_t d_size;

    public:
        HTable(size_t size): d_size(size) {
            data.resize(d_size);
            positions_in_use.resize(d_size);
        };

    //hashing function to convert strings into indexes
    int hash(string key) {
        int hash = 0;
        int index;
        for (size_t i = 0; i < key.length(); i++) {
            hash = hash + (int) key[i];
        }
        index = hash % d_size;
        return index;
    };

    //Function to insert pairs into hash table using hash function    

    bool insert(const string & key,
        const T & value) {
        int index;
        pair < string, T > emptypair;
        index = hash(key);
        int pos = index;
        if (data.at(index) != emptypair) {
            typename vector < pair < string, T >> ::iterator it;
            data.insert(it + index, pair < string, T > (key, value));
            positions_in_use[index] = true;
            cout << "linear";
            return true;
        } else {
            bool foundplace = false;
            while (!foundplace) {
                pos++;
                if (data.at(index) == emptypair) {
                    typename vector < pair < string, T >> ::iterator it;
                    data.insert(it + pos, pair < string, T > (key,value));
                    positions_in_use[pos] = true;
                    foundplace = true;
                    return foundplace;
                } else if (pos == index) throw runtime_error("Hash Table is full!");            
                if (pos == d_size - 1)
                    pos = 0;
            }
            return false;
        }
    }

    //Function to get the value of key that is stored into hash table
    T & get(const string & key) {
        int index = hash(key);
        int pos = index;
        if (data.at(index).first == key) {
            return data.at(index).second;
        } else {
            bool foundkey = false;
            while (!foundkey) {
                pos++;
                if (data.at(pos).first == key) {
                    foundkey = true;
                    return data.at(pos).second;
                } else if (pos == index) throw runtime_error("Not contained in Hash Table!");
                if (pos == d_size - 1)
                    pos = 0;

            }
        }
    }

    //erase vector pair at certain index in hash table
    void erase(const string & key) {
        pair < string, T > emptypair;
        int pos = hash(key);
        int index = pos;
        if (data.at(index).first == key) {
            data.at(index) = emptypair;
            positions_in_use[index] = 0;
        } else {
            index++;
            if (index = d_size)
                index = 0;
            else if (index = pos) throw runtime_error("Not contained in Hash Table!");
        }
    }

    //clear entire hash table    
    void clear() {
        data.clear();
        positions_in_use.clear();
    }

    //display all the hash table contents     
    friend ostream & operator << (ostream & os,
        const HTable & h) {
        for (size_t i = 0; i < h.data.size(); i++) {
            os << h.data[i].first << "," << h.data[i].second << endl;
        }
        return os;
    }
};

int main() {
  try {
    HTable < int > hs(10);
    int score = 43;
    string name = "peter";
    hs.insert(name, score);
    cout << hs;
  } catch (const exception & e) {
    std::cout << e.what();
  }
}

超时:受监视的命令转储核心 sh:第1行:141719分段故障超时10s

1 个答案:

答案 0 :(得分:0)

data.insert(it + index, ...)通过访问未初始化的变量it

表现出未定义的行为