Boost per ogni problema
-
24-10-2019 - |
Domanda
std::map< std::string , std::string > matrix_int;
typedef std::pair< std::string , std::string > lp_type;
BOOST_FOREACH( lp_type &row, matrix_int ){
}
questo non può essere rispettato: errore C2440: 'inizializzazione': impossibile convertire da 'std :: coppia <_Ty1, _Ty2>' a 'lp_type &'
quando ho '' nel tipo di elemento, spinta doc dice che posso usare typedef o predefinire una var; ma cosa devo fare quando voglio ottenere un riferimento?
Soluzione
Il tuo typedef non è corretto; ha bisogno di essere:
typedef std::pair< const std::string , std::string > lp_type;
^ note the added const
L'elemento chiave nella coppia mappa è const-qualificato.
Sarebbe un po 'più pulito di utilizzare il typedef value_type
; in questo modo di non ripetere le informazioni sul tipo:
typedef std::map<std::string, std::string> map_t;
map_t matrix_int;
BOOST_FOREACH(map_t::value_type& row, matrix_int){
}
Altri suggerimenti
E 'possibile utilizzare boost :: foreach con std: :?. mappa
Sembra che si devono fare:
typedef std::map< std::string, std::string > MyMap;
BOOST_FOREACH( MyMap::value_type& row, matrix_int ) {
}
Credo che James McNellis è giusto. Io aggiungo il suggerimento di approfittare della value_type
typedef che std :: map fornisce. Poi il codice potrebbe essere la seguente:
typedef std::map< std::string , std::string > MyMap;
MyMap matrix_int;
BOOST_FOREACH( MyMap::value_type &row, matrix_int ){
}