0% found this document useful (0 votes)
24 views

Map Exercises

Uploaded by

banhmihaichuc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Map Exercises

Uploaded by

banhmihaichuc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Map Exercises

https://www.cplusplus.com/reference/map/map/

1. Prepare a chart comparing the following features for the associative STL containers.

Container Feature Set Map


For each container:
 Describe the container and how it is stored in memory
 What are the advantages of using the container
What base type can the container store?
Does the container store elements in contiguous memory?
Does the container auto resize as needed?
Does the container permit fast insertions and removals?
Does the container support auto sorts?
Does the container store unique values?
Which types of iterators does the container have:
 Random access?
 Read-only iterators?
 Read and write iterators?
 Bidirectional iterators?
Does the container store a pair object?

2. How do you dereference an iterator for a set or map for read-only access, that is, getting the element’s value for
printing, assigning, or searching?

3. How do you dereference an iterator for a set or map for writing to an element stored in the container?

4. Write code to do the following processes. Use the sample file called Numbers1.txt, which contains int data.
a. Load a vector from the file.
b. Load a set from the vector.
c. Load a map from the vector, where the key is a number, and the mapped value is the frequency of the number.
d. Print the sizes of the vector, set, and map. Why are the sizes between the vector and set and map different?
What is the difference between the size of the vector and the map?
e. Print the map:
i. Using element access
ii. With iterators in a forward direction
iii. With iterators in a reverse direction
f. Print the map’s third element.
g. Print the lower half of the map.
h. Print the upper half of the map.
i. Query the map: is the value 99 in the map?
j. Update the map: add the value 1999 to the map.
k. Update the map: add one to the map’s key value 1999

5. Use the map from the previous question. Use any <STL container> operations or <algorithm> functions to help you
find the following stats about the numeric data set loaded from Numbers1.txt.
a. total number of elements
b. min and max (use the map’s key)
c. mean value (use the map’s key)
d. middle value (use the map’s key) – simply divide the map’s size by 2, regardless of whether the size is even or odd
e. what is the highest frequency value
f. what are the keys with the highest frequency value (mode)
g. how many unique numbers are in the map (that is, how many numbers occur just one time)
h. remove all odd numbers (use the map’s key)
i. insert the numbers stored in Numbers2.txt into the map
j. how many new elements were added to the map?

6. Create a map which will store a contact list for friends. Map from a contact to a phone number.

Joseph Greene Thomas Smith Teresa MacDonald Jennifer Adams Calvin Hurst
1-519-200-7898 1-519-685-8532 1-200-699-5689 1-519-566-1222 1-800-400-4566

Print the map. Query the map for a contact, using his/her first name. Output the
Chasing Coral, 4
contact’s phone number (using the format shown above) if it’s found in the map;
Planet of the Apes, 4
otherwise print an error message.
Wish Upon, 3
Chasing Coral, 4
7. Challenge Question
The Big Sick, 3
Blind, 3
Assume you have collected a text file of movie ratings where each movie is rated from 1
Planet of the Apes, 5
(bad) to 5 (excellent). A movie can have multiple ratings.
Lady Macbeth, 2
Chasing Coral, 2
See file called MovieRatings.txt; data shown opposite. Each rating is stored on one line,
Blind, 3
where the movie name is separated from its rating by a comma.
Spider-Man, 5
Despicable Me, 4
Write a program that reads in the file data. Create a struct called Rating and a map
Lady Macbeth, 4
called: map<string,Rating> m
Spider-Man, 4
Planet of the Apes, 3
Rating contains two attributes: ratingCount, ratingSum
Wish Upon, 2
The Big Sick, 4
Stream the file data and load the map with the movie as the key and a Rating object as Planet of the Apes, 4
the associated value. With every read from the file, locate an existing movie and update Planet of the Apes, 5
pair data OR create a new pair.

Write an << overload to print Rating data using the format shown below. Averages are rounded. Print the map.

Here is the desired output for the sample data, shown below.

Query the map for a movie, and output its rating data, if it’s found in the map; otherwise print an error message.

You might also like