Skip to main content
Rollback to Revision 3
Source Link
user34073
user34073

EDIT AFTER COMMENTS

Ok, after given answers and comments I ended up with something like this:

template< typename Container >
std::string to_query_params(Container&& container)      // 1.
{
    if (container.empty()){
        return "";
    }
    std::string result;
    for( const auto& item : container ) {
        result.append("&")
              .append(std::get<0>(item))
              .append("=")
              .append(std::get<1>(item));               // 2.
    }
    assert(result.size() > 0);
    result[0]='?';
    return result;
}
  • we're independent of underlying container, be it multimap, map, vector<tuple<string,string>>, etc;
  • and we're independent of r/lvalueness of param being able to call to_query_params(generate_params());
  • as of the container - I'll opt for std::vector<std::tuple<std::string, std::string>>, as we do not need to access elements, but iterate over them, so linear memory block will be cache-friendly;
    • thank's to append we're not creating useless copies;
  • std::stringstream is hudge and heavy, and I tend to avoid it as much as I can;

EDIT AFTER COMMENTS

Ok, after given answers and comments I ended up with something like this:

template< typename Container >
std::string to_query_params(Container&& container)      // 1.
{
    if (container.empty()){
        return "";
    }
    std::string result;
    for( const auto& item : container ) {
        result.append("&")
              .append(std::get<0>(item))
              .append("=")
              .append(std::get<1>(item));               // 2.
    }
    assert(result.size() > 0);
    result[0]='?';
    return result;
}
  • we're independent of underlying container, be it multimap, map, vector<tuple<string,string>>, etc;
  • and we're independent of r/lvalueness of param being able to call to_query_params(generate_params());
  • as of the container - I'll opt for std::vector<std::tuple<std::string, std::string>>, as we do not need to access elements, but iterate over them, so linear memory block will be cache-friendly;
    • thank's to append we're not creating useless copies;
  • std::stringstream is hudge and heavy, and I tend to avoid it as much as I can;
New code after review.
Source Link
jaor
  • 111
  • 1
  • 5

EDIT AFTER COMMENTS

Ok, after given answers and comments I ended up with something like this:

template< typename Container >
std::string to_query_params(Container&& container)      // 1.
{
    if (container.empty()){
        return "";
    }
    std::string result;
    for( const auto& item : container ) {
        result.append("&")
              .append(std::get<0>(item))
              .append("=")
              .append(std::get<1>(item));               // 2.
    }
    assert(result.size() > 0);
    result[0]='?';
    return result;
}
  • we're independent of underlying container, be it multimap, map, vector<tuple<string,string>>, etc;
  • and we're independent of r/lvalueness of param being able to call to_query_params(generate_params());
  • as of the container - I'll opt for std::vector<std::tuple<std::string, std::string>>, as we do not need to access elements, but iterate over them, so linear memory block will be cache-friendly;
    • thank's to append we're not creating useless copies;
  • std::stringstream is hudge and heavy, and I tend to avoid it as much as I can;

EDIT AFTER COMMENTS

Ok, after given answers and comments I ended up with something like this:

template< typename Container >
std::string to_query_params(Container&& container)      // 1.
{
    if (container.empty()){
        return "";
    }
    std::string result;
    for( const auto& item : container ) {
        result.append("&")
              .append(std::get<0>(item))
              .append("=")
              .append(std::get<1>(item));               // 2.
    }
    assert(result.size() > 0);
    result[0]='?';
    return result;
}
  • we're independent of underlying container, be it multimap, map, vector<tuple<string,string>>, etc;
  • and we're independent of r/lvalueness of param being able to call to_query_params(generate_params());
  • as of the container - I'll opt for std::vector<std::tuple<std::string, std::string>>, as we do not need to access elements, but iterate over them, so linear memory block will be cache-friendly;
    • thank's to append we're not creating useless copies;
  • std::stringstream is hudge and heavy, and I tend to avoid it as much as I can;
edited tags
Link
200_success
  • 145.6k
  • 22
  • 191
  • 481
added 5 characters in body; edited tags
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238
Loading
Source Link
jaor
  • 111
  • 1
  • 5
Loading