Kodewick
Jetzt lernen →
Grundlagen←→0 / 60

Wörter zählen

#003Grundlagenseen[w]++5 Min.

Schreib count_words(words), das eine map zurückgibt, wie oft jedes Wort vorkommt.

Beispiele
count_words({"cat", "dog", "cat"})→{'cat': 2, 'dog': 1}
count_words({})→{}
Die Funktionmap<string, int> count_words(const vector<string> &words)
main.cpp
#include <map>
#include <string>
#include <vector>
using namespace std;

map<string, int> count_words(const vector<string> &words) {
    map<string, int> seen;
    for (const string &w : words) {
        // dein Code hier: zähl 1 zur Anzahl dieses Worts dazu
    }
    return seen;
}
Strg + Enter führt den Code auch aus
Ergebnis

Ausführen testet die Beispiele. Wenn sie passen, drück auf Abgeben.