Hello, I am using a C++0x lambda expression to modify values of a map.
However, having difficulty passing the map iterator by reference.
If I just pass the iterator, by value such as: [](std::pair<TCHAR, int > iter)
it compiles fine, but the values does not get updated in the map.
If I try to pass the iterator by reference, such as [](std::pair<TCHAR, int >& iter)
the VS2010 compiler complains that it
cannot convert paramater from 'std::pair<_Ty1,_Ty2>' to 'std::pair<_Ty1,_Ty2> &'
Here is the code. Appreciate information on how the std::map objects can be modified using the lambda expressions.
#include <tchar.h>
#include <map>
#include <algorithm>
#include <vector>
int _tmain(int argc, _TCHAR* argv[])
{
typedef std::map<TCHAR, int > Map;
Map charToInt;
charToInt[_T('a')] = 'a';
charToInt[_T('b')] = 'b';
charToInt[_T('c')] = 'c';
charToInt[_T('d')] = 'd';
std::for_each(charToInt.begin(), charToInt.end(), [](std::pair<TCHAR, int >& iter)
{
int& val = iter.second;
val++;
});
return 0;
}
Thank you