C++ Hashing Problem please use the prime number 17011 for the size of your hasht
ID: 3732541 • Letter: C
Question
C++ Hashing Problem
please use the prime number 17011 for the size of your hashtable
Database file: Superhero Database
https://raw.githubusercontent.com/irawoodring/263/master/assignments/marvel-wikia-data.csv
For this project you are tasked with creating a hashmap class. It can be generic if you like (probably easier to write it that way), or you can write it specifically to house objects of type Superhero .A Superhero has the following fields int page_id; std::string name; std::string urlslug; std::string id; std::string alignment: char eye_color; char hair_color; char sex; std::string gsm; boolean alive; int appearances; std::string first appearance; int year; As per object oriented best practices all fields should be private Your hashmap class should hash based on the entire name of the superhero. You will use the separate chaining method to handle collisions (each array indice will correspond to a vector object where the data will be stored). Your hashmap will provide only the following public methods boolean insert (const Superhero & s); Superhero & get(const std::string name); The insert function returns either true or false depending on whether a collision occurred on insert. Regardless of whether a collision occurred or not the object should be inserted in the vector. Additionally, as we discussed in class, the object should be added to the front of the vector.Explanation / Answer
code:-
Represent the c++ program.
main.cpp
#include <vector>
#include <fstream>
#include <string>
#include <iostream>
#include <sstream>
#include "Superhero.h"
int main(int argc, char** argv){
std::ifstream in{"marvel-wikia-data.csv"};
std::vector<Superhero>* heroDB = new std::vector<Superhero>;
if(not in){
std::perror("File Error");
return 0;
}
std::string input;
//Get rid of the first line
std::getline(in,input);
while(std::getline(in,input)){
std::string params[13];
std::stringstream data(input);
for(int i(0); i < 13; i++)
std::getline(data, params[i], ',');
int page_id = std::stoi(params[0]);
const std::string name(params[1]);
const std::string urlslug(params[2]);
const std::string id(params[3]);
const std::string alignment(params[4]);
char eye_color('V');
if(!params[5].empty())
eye_color = params[5].at(0);
char hair_color('V');
if(!params[6].empty())
hair_color = params[6].at(0);
char sex('U');
if(!params[7].empty())
sex = params[7].at(0);
const std::string gsm(params[8]);
int appearances(1);
if(!params[10].empty())
appearances = std::stoi(params[10]);
const std::string first_apperance(params[11]);
int year(0);
if(!params[12].empty())
year = std::stoi(params[12]);
bool alive(true);
if(params[9].at(0) == 'D')
alive = false;
Superhero hero(page_id, name, urlslug, id, alignment, eye_color,
hair_color, sex, gsm, alive, appearances, first_apperance, year);
heroDB->push_back(hero);
}
for(auto hero_it(heroDB->begin()); hero_it < heroDB->end(); hero_it++){
Superhero output(*hero_it);
std::cout << output.toString() << std::endl;
}
}
Superhero.cpp
#include <string>
#include <sstream>
#include "Superhero.h"
Superhero::Superhero(int page_id, const std::string &name, const std::string &urlslug, const std::string &id,
const std::string &alignment, char eye_color, char hair_color, char sex, const std::string &gsm,
bool alive, int appearances, const std::string &first_apperance, int year) :
page_id(page_id), name(name),urlslug(urlslug), id(id), alignment(alignment), eye_color(eye_color),
hair_color(hair_color), sex(sex), gsm(gsm), alive(alive), appearances(appearances), first_apperance(first_apperance),
year(year) {}
std::string Superhero:: toString(){
return name;
}
//void Superhero::setPageID(int &page_id){
// Superhero::page_id = page_id;
//}
//
//int Superhero::getPageID() const{
// return page_id;
//}
//void Superhero::setName(std::string &name){
// Superhero::name = name;
//}
//
//std::string Superhero::getName() const{
// return name;
//}
//
//void Superhero::setUrlSlug(std::string &urlslug){
// Superhero::urlslug = urlslug;
//}
//
//std::string Superhero::getUrlSlug() const{
// return urlslug;
//}
//
//void Superhero::setID(std::string &id){
// Superhero::id = id;
//}
//
//std::string Superhero::getID() const{
// return id;
//}
//
//void Superhero::setAlighment(std::string &alignment){
// Superhero::alignment = alignment;
//}
//
//std::string Superhero::getAlignment() const{
// return alignment;
//}
//
//void Superhero::setEyeColor(char &eye_color){
// Superhero::eye_color = eye_color;
//}
//
//char Superhero::getEyeColor() const{
// return eye_color;
//}
//
//void Superhero::getHairColor(char &hair_color){
// Superhero::hair_color = hair_color;
//}
//
//char Superhero::getHairColor() const{
// return hair_color;
//}
//
//void Superhero::setSex(char &sex){
// Superhero::sex = sex;
//}
//
//char Superhero::getSex() const{
// return sex;
//}
//
//void Superhero::setGSM(std::string &gsm){
// Superhero::gsm = gsm;
//}
Superhero.h
#ifndef _H_SUPERHERO
#define _H_SUPERHERO
#include <string>
class Superhero{
public:
Superhero(int page_id, const std::string &name, const std::string &urlslug, const std::string &id,
const std::string &alignment, char eye_color, char hair_color, char sex, const std::string &gsm,
bool alive, int appearances, const std::string &first_apperance, int year);
std::string toString();
// void setPageID(int &page_id);
// int getPageID() const;
// void setName(std::string &name);
// std::string getName() const;
// void setUrlSlug(std::string &urlslug);
// std::string getUrlslug() const;
// void setID(std::string &id);
// std::string getID() const;
// void setAlignment(std::string &alignment);
// std::string getAlignment() const;
// void setEyeColor(char &eye_color);
// char getEyeColor() const;
// void setHairColor(char &hair_color);
// char getHairColor() const;
// void setSex(char &sex);
// char getSex() const; //should this be changed to be less dirty?
// void setGSM(std::string &gsm);
// std::string getGSM() const;
// void setAlive(bool &alive);
// bool getAlive() const;
// void setApperances(int &appearances);
// int getApperances() const;
// void setFirstApperance(std::string &first_apperance);
// std::string getFristApperance() const;
// void setYear(int &year);
// int getYear() const;
private:
int page_id;
std::string name;
std::string urlslug;
std::string id;
std::string alignment;
char eye_color;
char hair_color;
char sex;
std::string gsm;
bool alive;
int appearances;
std::string first_apperance;
int year;
};
#end
If one.text
page_ id, name,
urlslug,
ID,
ALIGN,
EYE,
HAIR,
SEX,
GSM,
ALIVE,
APPEARANCES,
FIRST APPEARANCE, Year
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.