In C++ implement an “associative array” that uses strings instead of integer indexes for keys and stores values of type double. Overload the index operator (operator[]) to provide access as in the following example: AssociativeArray prices; prices["Toaster Oven"] = 19.95;
then
Turn the AssociativeArray class into a template AssociativeArray<K, V>. Use a vector<pair<K, V>> to hold the key/value pairs. You don’t have to overload the [] operator. Instead, provide member functions put and get:
AssociativeArray<string, double> prices;
prices.put("Toaster Oven", 19.95);
prices.put("Car Vacuum", 24.95);
cout << prices.get("Toaster Oven") << endl;
operator[] needs to return a double&
prices["Car Vacuum"] = 24.95; cout << prices["Toaster oven"] << endl;
#include <bits/stdc++.h> using namespace std; int main() { map<string, double> prices{};// declaring associative array prices. prices["Car Vaccum"]=24.95; // Car Vaccum is the index or key and 24.95 is the value. prices["Toaster oven"]=32.75; cout << prices["Toaster oven"] << endl; cout << prices["Washing Machine"] << endl; // here washing machine is not declared in the associative array prices so this will print 0. }
Output
32.75 0
If you found this helpful please upvote
In C++ implement an “associative array” that uses strings instead of integer indexes for keys and...
I need one file please (C++)(Stock Market) Write a program to help a local stock trading company automate its systems. The company invests only in the stock market. At the end of each trading day, the company would like to generate and post the listing of its stocks so that investors can see how their holdings performed that day. We assume that the company invests in, say, 10 different stocks. The desired output is to produce two listings, one sorted...