Containers in Practice
Map Count Update
Maps associate keys with values, so repeated updates can accumulate a count for one key.
Map Count Update
map_count_update.cpp
#include <iostream>
#include <map>
#include <string>
int main() {
std::string item = ;
std::map<std::string, int> counts;
counts[item] += 1;
counts[item] += 1;
int count = counts[item];
std::cout << "item=" << item << std::endl;
std::cout << "count=" << count << std::endl;
return 0;
}
#include <iostream>
#include <map>
#include <string>
int main() {
std::string item = ;
std::map<std::string, int> counts;
counts[item] += 1;
counts[item] += 1;
int count = counts[item];
std::cout << "item=" << item << std::endl;
std::cout << "count=" << count << std::endl;
return 0;
}
map
A `std::map` stores key-value pairs ordered by key.
keyed update
Using `counts[key]` creates a value when the key is missing and then lets the program update it.