Skip to content

Instantly share code, notes, and snippets.

@MAGANER
Created March 16, 2023 04:11
Show Gist options
  • Save MAGANER/8df6dbb7e103d8ca425cb37ed205e824 to your computer and use it in GitHub Desktop.
Save MAGANER/8df6dbb7e103d8ca425cb37ed205e824 to your computer and use it in GitHub Desktop.
fast std::string concat c++
//originally it was written on
template <class... S>
std::string concat(const S&... strs) {
std::string_view views[]{strs...};
int totalsize = std::accumulate(std::begin(views), std::end(views), 0, [](int count, std::string_view v){return count + v.size();});
std::string result(totalsize, '\0');
int cur_pos = 0;
for(const auto& v : views){
std::copy(v.begin(), v.end(), result.begin() + cur_pos);
cur_pos += v.size();
}
assert(cur_pos == totalsize);
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment