【发布时间】:2015-11-26 20:49:08
【问题描述】:
我想将存储在地图中的所有单词更改为小写。使用 lambda 函数和变换,我该怎么做?
std::map <string, int> M;
std::map<string, int> M1;
std::transform(M.begin(), M.end(), M1.begin(),
[](pair<const string, int> const & p) { ::tolower(p.first); });
【问题讨论】:
-
您不能更改密钥。您应该将原始地图复制到其他地图中。
-
由于您的地图中的
string是键,当您访问地图中的元素时,您只有const访问权限(注意您的地图中的const string对),因此您无法编辑它们。您必须使用小写键创建一个新地图 -
@VladfromMoscow 喜欢这个?我已经编辑了我的问题。
-
只需对项目使用正常迭代来填充 M1。这里不需要花哨的东西。
标签: c++ algorithm dictionary lambda transform