Reputation: 30006
Assume I have the following function:
std::string greeting(const std::string_view name){
return std::string(name) + " we wish you a very nice day and week and month and year";
}
It works fine, but I want to avoid more than 1 memory allocation, assuming name is too long to fit into SSO buffer. So I came up with this:
std::string greeting_efficient(const std::string_view name){
constexpr std::string_view suffix = " we wish you a very nice day and week and month and year";
std::string result;
result.reserve(name.size()+suffix.size());
result+=name;
result+=suffix;
return result;
}
AFAIK it works but it is quite ugly, is there a easier way to do this?
I am fine with using C++20/23 if solution needs it.
Upvotes: 1
Views: 1188
Reputation: 117288
I'm not sure if it's more elegant or not, but this should assure that there's only one allocation:
std::string
with the correct size instead of default constructing it and reserving space. Expected effects:
name
directly into the string's memory.suffix
directly into the string's memory.
memcpy
s or memmove
s will likely be the result.#include <algorithm>
std::string greeting(std::string_view name){
static constexpr std::string_view suffix = " we wish you a very nice day"
" and week and month and year";
std::string rv(name.size() + suffix.size(), '\0'); // one allocation
std::copy(suffix.begin(), suffix.end(),
std::copy(name.begin(), name.end(), rv.begin()));
return rv;
}
Upvotes: 1
Reputation: 5503
You could use std::format
which is a C++20
feature. Or you could pull in the fmt library which is what std::format
is based off of if your compiler hasn't implemented std::format
yet.
#include <iostream>
#include <format>
#include <string>
static std::string greeting(std::string_view name)
{
return std::format("{} have a very nice day", name);
}
int main()
{
std::cout << greeting("Bill") << '\n';
}
You could also use an std::stringstream
as well, but I'm unsure how efficient it is.
static std::string greeting(std::string_view name)
{
std::ostringstream os{};
os << name << " have a very nice day";
return os.str();
}
Upvotes: 3