Created
March 16, 2023 04:11
-
-
Save MAGANER/8df6dbb7e103d8ca425cb37ed205e824 to your computer and use it in GitHub Desktop.
fast std::string concat c++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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