This question already has an answer here:
Consider the following code:
int main() {
list<string> names = { "ben", "ron", "dan" };
char c = 'n';
map<string, int> filtered;
for_each(names.begin(), names.end(),
[&filtered, c](const string& s) {
if (s.find(c) != string::npos) { filtered[s]++; }
});
cout << "size is: " << filtered.size() << endl;
for (auto& nameCountPair : filtered) {
const string name = nameCountPair.first;
cout << name << endl;
}
}
When I remove the &
inside the capture (i.e. [filtered, c]
) I get the error:
no operator "[]" matches these operands
Why?
Aucun commentaire:
Enregistrer un commentaire