Vectors and Maps – Examples

Page 11 of 11 · Basic examples

The tasks on this web page were chosen so that you can practise the lessons vectors in C++ and maps in C++.

Parts of harder tasks that need vectors and maps to be solved in the most efficient way have also been picked out.

The harder tasks are mostly tasks from competition preparation:

Tasks

Before you start: read the lessons Vector (dynamic array), Maps in C++.
How to practise: first try to solve the task on your own (at least on paper), then open the hint, and the solution only at the end. The solutions are most often in C or C++; if you are learning Java or Python, rewrite the idea in your language.
TASK 01

Printing a map

We are given data about the salaries of workers:
Petar Petrovic 55000 RSD
Steva Đurđev 100000 RSD
Milica Trajkovski 97000 RSD

Using these salary data, create a map that contains key–value pairs, so that the names of the workers are the keys. Print all the data on the screen.

TASK 02

Above-average grades

Read the number of students, then the names of the students and their average grades, and store them in a map so that the names of the students are the keys. On the output print a table with the names of the students in one column and their averages in the other, only for the students whose average is greater than the average of the whole group.

Example:
Input
4
Pera Perić 4.45
Mika Mikić 2.89
Sara Matić 4.89
Nikola Terzić 3.90

Output:
Pera Perić 4.45
Sara Matić 4.89

(The average of the group is 4.03, so only Pera and Sara are above it.)

TASK 03

A trainers offer

A trainers shop has n different models of trainers in stock, described by their name and price. Read n, and then read the models and prices of the trainers into a map.
Print the names of the trainers and their prices from the most expensive to the cheapest. If several trainers have the same price, sort them in lexicographic order.

Example 1:
Number of trainer models: 3
Enter the trainer model and the price:
Nike 5000
Enter the trainer model and the price:
Adidas 4500
Enter the trainer model and the price:
Puma 4000

Output:
Nike 5000
Adidas 4500
Puma 4000

Example 2:
Number of trainer models: 4
Enter the trainer model and the price:
Converse 3000
Enter the trainer model and the price:
Vans 3500
Enter the trainer model and the price:
NewBalance 3200
Enter the trainer model and the price:
Reebok 3400

Output:
Vans 3500
Reebok 3400
NewBalance 3200
Converse 3000

TASK 04

The largest daily takings (fiscal receipts)

THE TASK:
We know all the fiscal receipts issued by one shop. Each receipt shows the date when it was issued and the amount that was charged.
Write a program that finds the maximum daily takings of the shop.
Example 1:

Input:
Enter the number of fiscal receipts: 3
Enter the date and amount of receipt 1 (format: DD.MM.YYYY): 01.05.2024 1500
Enter the date and amount of receipt 2 (format: DD.MM.YYYY): 01.05.2024 2000
Enter the date and amount of receipt 3 (format: DD.MM.YYYY): 02.05.2024 1800

Output:
Maximum daily takings: 3500 dinars, date: 01.05.2024

Example 2:

Input:
Enter the number of fiscal receipts: 4
Enter the date and amount of receipt 1 (format: DD.MM.YYYY): 03.05.2024 1200
Enter the date and amount of receipt 2 (format: DD.MM.YYYY): 03.05.2024 1700
Enter the date and amount of receipt 3 (format: DD.MM.YYYY): 04.05.2024 2200
Enter the date and amount of receipt 4 (format: DD.MM.YYYY): 05.05.2024 2500

Output:
Maximum daily takings: 2900 dinars, date: 03.05.2024

Common mistakes and tips
  • v.size() is an unsigned number: v.size() - 1 for an empty vector is not −1, but a huge number.
  • m[key] adds the key if it does not exist; to only check, use m.count(key) or m.find(key).
  • The elements of a map are always ordered by key; to sort by value, copy them into a vector<pair<...>>.

Check your knowledge

Open the quiz

Mini quiz: vectors and maps

1. Which statement adds an element to the end of a vector?




2. In what order does a map<string,int> store its elements?