Skip to content
Exercise · Containers and Iterators · lesson 10 of 11

Exercise: word frequencies

Write a function std::map<std::string, int> frequencies(const std::vector<std::string>& words). It returns a std::map whose keys are the words from words and whose values say how many times each word occurs there.

  • frequencies({"a", "b", "a"}) → {{"a", 2}, {"b", 1}}
  • frequencies({"x", "x", "x"}) → {{"x", 3}}
  • frequencies({}) → {}

Constraints: words are compared case-sensitively, so "Go" and "go" are different keys. The vector words may be empty.

In the tests, Counts is a short name for std::map<std::string, int>, set by the line using Counts = ….

Do not add extra #include lines unless the task says so explicitly.

frequencies.cpp — cpp
cpp · 8 lines
Ctrl+Enter run · Esc leave editor

Subscribe to take the course

The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.

Sign in another way