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.

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