Stock information data structure for c? -
i'm new c(less week), , trying figure out more effective way of retrieving data. imagine have few pieces of stock data: ticker, price, change.
my approach far has been put data in 3 arrays. if want price of ibm, search ticker array , index location location price array. works fine because lot of lookups , data doesn't change wondering if there maybe more effective way of doing this?
i tried dictionary/hashmap maybe store ibm key , array of price/change values, can't seem figure out how in c. if possible there simple way this? working on different program , don't want learn how create own scratch(although if have to, def. work on it).
try this: http://uthash.sourceforge.net/
the examples on front page pretty self explanatory.
struct stock { float price, change; char name[3]; ut_hash_handle hh; }; struct stock * stockshash = null; struct stock * stockitem; hash_add_str(stockshash, name, stockitem ); hash_find_str(stockshash, "ibm", stockitem ); //edit void new_stock(struct stock * stockshash, char *name, float price, float change) { struct stock *s; s = malloc(sizeof(struct stock)); strncpy (s->name, name, 3); s->price = price; s->change = change; s->id = user_id; hash_add_str( stockshash, name, s ); } void update_stock (struct stock * stockshash, char *name, float price, float change) { struct stock *s; hash_find_str (stockshash, name, s); if (s) { s->price = price; s->change = change; } }
Comments
Post a Comment