【发布时间】:2021-11-27 15:32:37
【问题描述】:
我正在浏览techiedelight 文章link
我没明白std::for_each(s.begin(), s.end(), [&m](char &c) { m[c]++; });中 [&m](char &c) { m[c]++; }的意思
#include <iostream>
#include <unordered_map>
#include <algorithm>
int main()
{
std::unordered_map<char, int> m;
std::string s("abcba");
std::for_each(s.begin(), s.end(), [&m](char &c) { m[c]++; });
char ch = 's';
if (m.find(ch) != m.end()) {
std::cout << "Key found";
}
else {
std::cout << "Key not found";
}
return 0;
}
请有人解释它是如何工作的。 提前致谢。
【问题讨论】:
标签: c++ string algorithm dictionary stl