带字符串键

时间:2017-11-12 00:46:38

标签: c++ hashtable

我是C ++中哈希表的新手,我一直在编写这个从文件中读取数据的程序。四位数的学生ID及其带嵌入空间的名称。当程序运行时,用户将输入选项1或2,如果他们选择了选项1,他们可以输入学生ID,并且会给他们学生ID或者说没有名字。

这是从文本文件中,它在文本文件中看起来不像这样。学生ID位于第一列,旁边有名称。

2300 Robb Arredondo 
5401 Chris Campos
6305 Yogi Bear
9108 Yoshi Man 
0310 John Du
1812 Maria Yu
4318 Power Ranger
7122 Bob Chan
8225 Will Boo
5324 Ghost Lee
0134 Mary Su 
2150 Jane Mary
1100 Gary Campos 
2305 Alan Kong 
3420 Bill Nye 
5608 Alex Garcia 
9112 Goku Nani 
6750 Paul Avalos 
1200 Jason Noni 
9005 Oscar Roger 
6550 Geo Qwerty
1112 Mini Me
2315 Garfield Beria
4201 Just Saying
2399 Help Me

本书使用整数对哈希表进行排序,但我需要使用字符串变量作为键来对哈希表进行排序。问题是,学生ID最后两位数将对哈希表进行排序,并将学生放入正确的位置。我不知道该怎么做,我需要一个字符串变量。我不能使用int。我已经读过你可以使用ascii值来对哈希表进行排序,但我不认为它可以在这里应用。

另外,我的析构函数存在问题。当我按下选项2退出程序时,它崩溃了。我为它注释了代码。我正在使用链接来解决与链表的冲突。

我尝试在main中使用atoi函数将字符串转换为整数来解决问题,但它没有为学生返回正确的名称。最后一件事,我可以使用这个哈希表的向量吗?与代替成员数组一样,它是一个Student*作为其类型的向量

#pragma once
#include <string>

struct Student
{
std::string m_idNum;
std::string m_Name;
Student *next;
};

#pragma once
#include "Student.h"
#include <iostream>

class HashTable
{

  private:
     int Hash(const std::string&);
     static const int tableSize = 100;
     Student* listofStudents[tableSize];

   public:
      HashTable();
     ~HashTable();
      void Insert(std::string ID, std::string name);
      void Retrieve(std::string ID);
 };

#include "HashTable.h"


HashTable::HashTable()
{
    for (int i = 0; i < tableSize; i++)
    {
        listofStudents[i] = new Student;
        listofStudents[i]->m_Name = " ";
        listofStudents[i]->m_idNum = " ";
        listofStudents[i]->next = NULL;
    }
}

HashTable::~HashTable()
{
    /*for (int i = 0; i < tableSize; i++)
    {
        Student* ptr = listofStudents[i];
        while (ptr != NULL)
        {
            Student* prev = ptr;
            ptr = ptr->next;
            delete prev;
        }
    }

    delete[] listofStudents;*/
}

void HashTable::Insert(std::string ID, std::string name)
{
    int location = Hash(ID);

    if (listofStudents[location]->m_Name == "")
    {
        listofStudents[location]->m_Name = name;
        listofStudents[location]->m_idNum = ID;
    }
    else
    {
        Student* ptr = listofStudents[location];
        Student* newStudent = new Student;
        newStudent->m_Name = name;
        newStudent->m_idNum = ID;
        newStudent->next = NULL;

        while (ptr->next != NULL)
        {
            ptr = ptr->next;
        }
        ptr->next = newStudent;
    }

}

void HashTable::Retrieve(std::string ID)
{
    int location = Hash(ID);
    bool foundStudent;
    std::string name;

    Student* ptr = listofStudents[location];
    while (ptr != NULL)
    {
        if (ptr->m_idNum == ID);
        {
            foundStudent = true;
            name = ptr->m_Name;
        }
        ptr = ptr->next;
    }

    if (foundStudent == true)
    {
        std::cout << "------------------------------\n";
        std::cout << " Name of Student: " << name << std::endl;
        std::cout << "------------------------------\n";
    }
    else
    {
        foundStudent = false;
        std::cout << "---------------------------------\n";
        std::cout << " No Student exist with that ID" << std::endl;
        std::cout << "----------------------------------\n";
    }

}

int HashTable::Hash(const std::string& key)
{
    int sum = 0;

    for (int index = 0; index < key.length(); index++)
        sum = 39 * sum + key[index];

    sum %= tableSize;

    if (sum <0)
        sum += tableSize;

    return sum;
}



  #include <fstream>
#include "HashTable.h"

int Menu();
void FindStudent(HashTable&);

int main()
{
    HashTable hashtable;
    std::ifstream file("students.txt");
    int option;
    std::string studentID;
    std::string studentName;

    while (file >> studentID >> studentName)
    {
        hashtable.Insert(studentID, studentName);
    }

    do
    {
        option = Menu();

        switch (option)
        {
            case 1: FindStudent(hashtable);
            break;

            case 2:
            break;
        }

    } while (option != 2);

    return 0;
}

int Menu()
{
    int choice;

    std::cout << " 1.)Find Student by ID\n";
    std::cout << " 2.)Exit\n";
    std::cout << " Enter option:";
    std::cin >> choice;

    return choice;
}

void FindStudent(HashTable& hash)
{
    std::string fourdigit;

    std::cout << " Enter Students four digit ID: ";
    std::cin >> fourdigit;

    std::atoi(fourdigit.c_str());

    hash.Retrieve(fourdigit);
}

1 个答案:

答案 0 :(得分:0)

在析构函数中,delete[] listofStudents;错误,导致崩溃。

您正在实施hastable,但是您将学生实现为链接列表,这在此程序中没有用。您可以从Student *next结构和所有相关代码中删除Student成员,然后更改delete[] listofStudents;,如下所示,它将起作用。

//delete[] listofStudents;//Second deletion, this is wrong.
for (int i = 0; i < tableSize; i++)
{
    delete listofStudents[i];
}