Me gustaría iterar sobre un estándar :: map usando BOOST_FOREACH y edito los valores. No puedo entenderlo.usando BOOST_FOREACH con std :: map
typedef std::pair<int, int> IdSizePair_t;
std::map<int,int> mmap;
mmap[1] = 1;
mmap[2] = 2;
mmap[3] = 3;
BOOST_FOREACH(IdSizePair_t i, mmap)
i.second++;
// mmap should contain {2,3,4} here
Por supuesto, esto no cambia nada porque no estoy iterando por referencia. Así sustituyo esta línea en su lugar (como en el ejemplo en la documentación Boost):
BOOST_FOREACH(IdSizePair_t &i, mmap)
y consigo el error del compilador:
error C2440: 'initializing' :
cannot convert from 'std::pair<_Ty1,_Ty2>' to 'IdSizePair_t &'
with
[
_Ty1=const int,
_Ty2=int
]
¿Alguna sugerencia?
Qué compilador está usando? Probé tu código en VS2008 y funcionó correctamente. Probé hvint's [answer] (http://stackoverflow.com/questions/795443/using-boostforeach-with-stdmap/795482#795482) también y funcionó. Estoy usando boost 1.36, si eso importa. –
es probable que haya olvidado el &? sin eso, copia el otro par, y la constness no importará entonces. –