Slightly confused and I couldn't find anything pertaining to my question. I might be asking it the wrong way.
I have this code:
#include <iostream>
#include <string>
class AssociativeArray {
public:
AssociativeArray(){
for (int i = 0; i < tableSize; i++) {
HashTable[i] = new item;
HashTable[i]->name = "empty";
HashTable[i]->price = 0.00;
HashTable[i]->next = NULL;
}
}
int HashKey(std::string key) {
int hash = 0;
int index;
for (int i = 0; i < key.length(); i++) {
hash = hash + (int)key[i];
}
index = hash % tableSize;
return index;
}
void addItem(std::string name, double price) {
int index = HashKey(name);
if (HashTable[index]->name == "empty") {
HashTable[index]->name = name;
HashTable[index]->price = price;
}
else {
item* ptr = HashTable[index];
item* n = new item;
n->name = name;
n->price = price;
n->next = NULL;
while (ptr->next != NULL) {
ptr = ptr->next;
}
ptr->next = n;
}
}
double& findPrice(std::string name) {
int index = HashKey(name);
bool found = false;
item* ptr = HashTable[index];
item* price = ptr;
while (ptr != NULL) {
if (ptr->name == name) {
found = true;
price = ptr;
}
ptr = ptr->next;
}
if (found == true) {
return price->price;
}
else {
addItem(name, 0.00);
return price->price;
}
}
double& operator[](std::string name) {
return findPrice(name);
}
private:
static const int tableSize = 5;
struct item {
std::string name;
double price;
item* next;
};
item* HashTable[tableSize];
};
int main() {
AssociativeArray prices;
prices.addItem("Socks", 10.96);
std::cout << prices["Socks"] << std::endl;
prices["Socks"] = 7.77;
std::cout << prices["Socks"] << std::endl;
prices["Toaster Oven"] = 19.95;
std::cout << prices["Toaster Oven"] << std::endl; //Print 0.00, doesn't update price!
prices["Toaster Oven"] = 19.95; //update the price!?
std::cout << prices["Toaster Oven"] << std::endl;
system("PAUSE");
return 0;
}
Basically, what I am trying to make an array through hashing. I think I am overloading the [] operator wrong. For some reason the assignment is not allowing the item to update. Any ideas? Any help or just a push in the right direction would be helpful!
The way I have it now is when the object is not found when the operator[] is called, a new object is written into the hash for that item. Seen below:
while (ptr != NULL) {
if (ptr->name == name) {
found = true;
price = ptr;
}
ptr = ptr->next;
}
if (found == true) {
return price->price;
}
else {
addItem(name, 0.00);
return price->price;
}
But the assignment of the double value doesn't seem to kick in till after the object is made.
prices["Toaster Oven"] = 19.95;
std::cout << prices["Toaster Oven"] << std::endl; //Prints 0.00 Doesn't work
prices["Toaster Oven"] = 19.95;
std::cout << prices["Toaster Oven"] << std::endl; //Prints 19.95 works
Should I be doing this a different way? Any suggestions. Thanks a million.
Problem is here:
addItem(name, 0.00); // you construct new price item
return price->price; // but here you return ref to some other item.
check comments above.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.