My goal is to create a URL query params from std::map<std::string, std::string>
. For example, we call to_query_params( {{"one","1"},{"two","2"}} )
and we get ?one=1&two=2
.
Is this a good way of doing it?
std::string to_query_params(std::map<std::string, std::string> map)
{
if (map.empty()){
return "";
}
auto map_beg = std::begin(map);
auto concat = [](const decltype(map)::value_type& a){
return a.first+"="+a.second;
};
std::string result("?"+concat(*map_beg));
std::for_each(std::next(std::begin(map)),
std::end(map),
[&](decltype(map)::value_type param){
result+="&"+concat(param);
});
return result;
}