C++匿名函数(Lambda)引用传递无法修改MAP值问题
#include <map>
#include <algorithm>
#include <iostream>
int _tmain(int argc, _TCHAR* argv[])
{
int iValue[] = { 1, 2, 3, 4, 5 };
std::map<int, bool >cfgAcceptList;
std::for_each(std::begin(iValue), std::end(iValue), [&](int index){ cfgAcceptList[index] = false; });
std::for_each(std::begin(cfgAcceptList), std::end(cfgAcceptList),
[&](std::pair<int, bool> list){
std::cout << "赋值前Key:" << list.first << "Value:" << list.second << std::endl;
});
std::cout << std::endl;
std::for_each(std::begin(cfgAcceptList), std::end(cfgAcceptList),
[&](std::pair<int, bool> list){
list.second = true;
});
std::cout << std::endl;
std::for_each(std::begin(cfgAcceptList), std::end(cfgAcceptList),
[&](std::pair<int, bool> list){
std::cout<< "赋值后Key:" << list.first << "Value:" << list.second << std::endl;
});
system("push");
return 0;
}
以上代码理论上在第二个for_each的时候会改变map的值,但是小弟这里测试发现值没有被改变。
请问是因为我这个匿名函数的值引用方式有问题么?该如何处理,请大家指正?
相关推荐
Lambda表达式里面修改外部变量问题
主要介绍了Lambda表达式里面修改外部变量的相关知识,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
Python之lambda匿名函数及map和filter的用法
现有两个元组((‘a’),(‘b’)),((‘c’),(‘d’)),请使用python中匿名函数生成列表[{‘a’:’c’},{‘b’:’d’}] t1 = (('a'), ('c')) t2 = (('b'), ('d')) print(list(map(lambda t: {t[0]: t[1]}, zip(t1, t2)))) l = lambda t1, t2: [{i: j} for i, j in zip(t1, t2)] print(l(t1, t2)) map内置函数使用: map(函数,数据源)—>函数的参数是数据源的每一个元素传入,比如: l = [1,2,3,4,5,
Python匿名函数(lambda表达式)
原文地址 我的简单理解就是通过一行代码定义一个函数 lambda表达式语法如下 函数名 = lambda 参数 : 参数表达式 在使用的时候 函数名(变量) eg. 通过常规方式定义函数 if x<y: res = x+y else: res = x-y return res print(fun(1,3)) # 4 通过lambda表达式定义一个函数 fun = lambda x, y: x+y if x<y else x-y print(fun(1,3)) # 4 lambda表达式与map函数结合方便的进行数据处理 map()
Python函数的返回值、匿名函数lambda、filter函数、map函数、reduce函数用法实例分析
主要介绍了Python函数的返回值、匿名函数lambda、filter函数、map函数、reduce函数用法,结合实例形式分析了Python函数的返回值、匿名函数lambda、filter函数、map函数、reduce函数相关功能、原理与使用技巧,需要的朋友可以参考下
详解Python匿名函数(lambda函数)
主要介绍了Python匿名函数(lambda函数),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧


