哈希表不会读取测试文件

时间:2017-05-31 19:05:49

标签: c++ c++11 vector hashtable

我被分配使用向量向量创建链式哈希表。它旨在保存Entry类型的对象。我已经编写了所有的函数和构造函数,但是当我尝试使用读取输入的构造函数然后按键的顺序将它输出给我时,无论哪个字符串都在.txt文件的最后一行,它的读取来自不见了。我认为这是我的构造函数的问题,因为当我尝试使用get从表中获取该特定值然后再打印它就消失了。我希望能从一些经验丰富的人身上找到一些可能出错的地方。谢谢。继承我的代码:

条目标题文件:

#ifndef entry_h
#define entry_h

// entry.h - defines class Entry            


#include <string>
#include <iosfwd>

class Entry {

public:
    // constructor                                          
    Entry(unsigned int key = 0, std::string data = "");

    // access and mutator functions                         
    unsigned int get_key() const;
    std::string get_data() const;
    static unsigned int access_count();
    void set_key(unsigned int k);
    void set_data(std::string d);

    // operator conversion function simplifies comparisons  
    operator unsigned int () const;

    // input and output friends                             
    friend std::istream& operator>>
    (std::istream& inp, Entry &e);
    friend std::ostream& operator<<
    (std::ostream& out, Entry &e);

private:
    unsigned int key;
    std::string data;
    static unsigned int accesses;
};

#endif /* entry_h */

表头文件:

//
//  table.h
//  
//
#ifndef table_h
#define table_h

#include <string>
#include <vector>
#include <algorithm>
#include "entry.h"

using namespace std;

class Table {

 public:
  Table(unsigned int max_entries = 100);
  //Builds empty table to hold 100 entries
  Table(unsigned int entries, std::istream& input);
  //Builds table to hold entries, reads and puts them 1 at a time
  ~Table();
  //Destructor
  void put(unsigned int key, std::string data);
  //Creates new entry for the table
  //Updates if key is used
  void put(Entry e);
  //Creates a copy of e in the table
  string get(unsigned int key) const;
  //Returns string associated with key
  //Returns empty string if key isnt used
  bool remove(unsigned int key);
  //Removes entry in given key
  //Returns true of removed, false of no entry
  int find(Entry e);
  //index in second array that e exists, 0 if not found
  friend std::ostream& operator<< (std::ostream& out, const Table& t);
  //Prints each entry in the table in order of their key


 private:
  int size;
  int num_entries;
  unsigned int hashkey(unsigned int key) const;

  vector<vector<Entry> > A;
};

#endif /* table_h */

表格实施文件:

//table.cpp

#include<iostream>
#include<vector>
#include<algorithm>
#include "table.h"

using namespace std;


Table::Table(unsigned int max_entries){
  max_entries = 100;
  size = max_entries * 2;
  A.resize(size);
}

Table::Table(unsigned int entries, std::istream& input){
  size = entries*2;
  A.resize(size);
  num_entries = entries;
  Entry temp;
  for (size_t i = 0; i < entries; i++) {
    input >> temp;
    put(temp);
  }
}

Table::~Table() {
  A.clear();
}

void Table::put(unsigned int key, std::string data){
  Entry e;
  e.set_key(key);
  e.set_data(data);
  put(e);
  num_entries++;
}

void Table::put(Entry e) {
  if (A[hashkey(e.get_key())].empty()) {
    A[hashkey(e.get_key())].push_back(e);
    }
  else {
    for(size_t i = 0; i < A[hashkey(e.get_key())].size(); i++) {
    if (A[hashkey(e.get_key())][i].get_key() == e.get_key()) {
      remove(A[hashkey(e.get_key())][i].get_key());
      break;
    }
      }
   A[hashkey(e.get_key())].push_back(e);
  } 
}

string Table::get(unsigned int key) const {
  if( A[hashkey(key)].size() == 0) {
    return "";
  }
  else {
    for (size_t i = 0; i < A[hashkey(key)].size(); i++) {
      if (A[hashkey(key)][i].get_key() == key) {
    return A[hashkey(key)][i].get_data();
      }
      else {
    return "";
      }
    }
  }
}

bool Table::remove(unsigned int key) {
  for (size_t i = 0; i < A[hashkey(key)].size(); i++) {
    if (A[hashkey(key)][i].get_key() == key) {
    swap(A[hashkey(key)][i],A[hashkey(key)][A[hashkey(key)].size() - 1]);
    A[hashkey(key)].pop_back();
    num_entries--; 
        return true;
      }
      else {
    return false;
      }
  }
}

int Table::find(Entry e) {
  for (size_t i = 0; i < A[hashkey(e.get_key())].size(); i++) {
      if (A[hashkey(e.get_key())][i] == e) {
    return i;
      }
      else {
    return 0;
      }
  }
}

ostream& operator << (ostream& out, const Table& t) {
  vector<Entry> order;
  for(size_t i = 0; i < t.A.size(); i++) {
    for (size_t j = 0; j < t.A[i].size(); j++) {
      order.push_back(t.A[i][j]);
    }
  }
  sort(order.begin(), order.end());
  for(Entry k: order) {
    out << k << endl;
  }
  return out;
}

unsigned int Table::hashkey(unsigned int key) const{
  const double c = 1.61803;
  // return key % size;
  return (int)(size*((key * c) - (int)(key * c)));
}

0 个答案:

没有答案