Put and get most recent stock price in a data structure.
Anonymous
One way might be this: Use a vector to store the symbols and the find to update them. #include #include #include #include #include // std::find using namespace std; struct stockPrice{ float high; float low; float current; time_t time; string symbol; bool operator==(const stockPrice& s) const { //overload for find return (symbol == s.symbol); } }; void addorUpdateprice(vector* legend, float current, string f_sym){ stockPrice s = {0,0,0,0,f_sym}; auto thisone = find(legend->begin(), legend->end(), s); thisone->current = current; thisone->high = (current > thisone->high) ? current : thisone->high; thisone->high = (current low) ? current : thisone->low; thisone->time = time(0); } float getprice(vector* legend, string f_sym){ stockPrice s = {0,0,0,0,f_sym}; auto thisone = find(legend->begin(), legend->end(), s); return thisone->current; } int main(int argc, char** argv) { vector prices; stockPrice s = {100.f, 50.f, 75.f, time(0),"goog"}; stockPrice s1 = {101.f, 51.f, 75.f, time(0),"appl"}; stockPrice s2 = {102.f, 52.f, 75.f, time(0),"dell"}; prices.push_back(s); prices.push_back(s1); prices.push_back(s2); addorUpdateprice(&prices,101.f, "goog"); float a = getprice(&prices,"goog"); return 0; }
Check out your Company Bowl for anonymous work chats.