vendredi 22 juin 2018

Iterate over map and use the pair as reference parameter C++11

I have an std::map. and I would like to iterate over it and use the result as an argument to a function. The compilation seems to complain about my object beeing a lvalue, but I cannot figure out why it is considered as a lvalue.

void my_function(std::pair<std::string, std::string>& my_arg){
    //do stuff, modify elements from the pair
}

std::map<std::string, std::string> my_map;
// fill the map with values...

for(auto& element : my_map){
    my_function(element);
}

I could probably use an iterator to solve this, but I would like to learn how to do it the c++11 way.

Thanks

Aucun commentaire:

Enregistrer un commentaire