- tags: C/C++
Capture a map with reference
If we capture a map by value, then we can’t use the operator []
:
unordered_map<int, int> freq;
// Won't compile
// auto comp_by_map = [freq](const int& a, const int& b) { return freq[a] < freq[b];};
auto comp_by_map = [&freq](const int& a, const int& b) { return freq[a] < freq[b];};