Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boost for each problem

std::map< std::string , std::string > matrix_int;
typedef std::pair< std::string , std::string > lp_type;
BOOST_FOREACH( lp_type &row, matrix_int ){

}

this can not be complied: error C2440: 'initializing' : cannot convert from 'std::pair<_Ty1,_Ty2>' to 'lp_type &'

when I have ',' in element type, boost doc says I can use typedef or predefine a var; but what should I do when I want to get a reference?

like image 611
areslp Avatar asked Jan 22 '26 04:01

areslp


1 Answers

Your typedef is incorrect; it needs to be:

typedef std::pair< const std::string , std::string > lp_type;
                   ^ note the added const

The key element in the map pair is const-qualified.

It would be a bit cleaner to use the value_type typedef; this way you don't repeat the type information:

typedef std::map<std::string, std::string> map_t;
map_t matrix_int;
BOOST_FOREACH(map_t::value_type& row, matrix_int){

}
like image 62
James McNellis Avatar answered Jan 24 '26 20:01

James McNellis