Question

In C++ implement an “associative array” that uses strings instead of integer indexes for keys and...

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;
0 0
Add a comment Improve this question Transcribed image text
Answer #1
 #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

Add a comment
Know the answer?
Add Answer to:
In C++ implement an “associative array” that uses strings instead of integer indexes for keys and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need one file please (C++)(Stock Market) Write a program to help a local stock trading...

    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...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT