title: Segmentation fault error in Hash table using vectors of pair data type
body: <p>I am trying to implement a hash table using vectors of pair data type.I am thinking I have implemented the principle working of hash table without any compile errors but when I try to run it is throwing segmentation fault error.</p>
  <h1>include </h1>
  <pre><code> #include &lt;stdexcept&gt;
  
   #include &lt;string&gt;
  
   #include &lt;vector&gt;
  
   using namespace std;
  
   template &lt; class T &gt;
    class HTable {
      private:
          vector &lt; pair &lt; string, T &gt;&gt; data;
      vector &lt; bool &gt; 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 &lt; 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 &amp; key,
          const T &amp; value) {
          int index;
          pair &lt; string, T &gt; emptypair;
          index = hash(key);
          int pos = index;
          if (data.at(index) != emptypair) {
              typename vector &lt; pair &lt; string, T &gt;&gt; ::iterator it;
              data.insert(it + index, pair &lt; string, T &gt; (key, value));
              positions_in_use[index] = true;
              cout &lt;&lt; &quot;linear&quot;;
              return true;
          } else {
              bool foundplace = false;
              while (!foundplace) {
                  pos++;
                  if (data.at(index) == emptypair) {
                      typename vector &lt; pair &lt; string, T &gt;&gt; ::iterator it;
                      data.insert(it + pos, pair &lt; string, T &gt; (key,value));
                      positions_in_use[pos] = true;
                      foundplace = true;
                      return foundplace;
                  } else if (pos == index) throw runtime_error(&quot;Hash Table is full!&quot;);            
                  if (pos == d_size - 1)
                      pos = 0;
              }
              return false;
          }
      }
  
      //Function to get the value of key that is stored into hash table
      T &amp; get(const string &amp; 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(&quot;Not contained in Hash Table!&quot;);
                  if (pos == d_size - 1)
                      pos = 0;
  
              }
          }
      }
  
      //erase vector pair at certain index in hash table
      void erase(const string &amp; key) {
          pair &lt; string, T &gt; 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(&quot;Not contained in Hash Table!&quot;);
          }
      }
  
      //clear entire hash table    
      void clear() {
          data.clear();
          positions_in_use.clear();
      }
  
      //display all the hash table contents     
      friend ostream &amp; operator &lt;&lt; (ostream &amp; os,
          const HTable &amp; h) {
          for (size_t i = 0; i &lt; h.data.size(); i++) {
              os &lt;&lt; h.data[i].first &lt;&lt; &quot;,&quot; &lt;&lt; h.data[i].second &lt;&lt; endl;
          }
          return os;
      }
  };
  
  int main() {
    try {
      HTable &lt; int &gt; hs(10);
      int score = 43;
      string name = &quot;peter&quot;;
      hs.insert(name, score);
      cout &lt;&lt; hs;
    } catch (const exception &amp; e) {
      std::cout &lt;&lt; e.what();
    }
  }
  </code></pre>
  <p>timeout: the monitored command dumped core
  sh: line 1: 141719 Segmentation fault      timeout 10s</p>