I'm implementing a HTTP server and the API I follow define the raw response data as a std::vector<std::byte>>.
I store http responses headers as std::string in my code and at some point I have to write them to to raw response data before sending it back.
The thing is, I cannot find a clean way to write/append data from a std::string to my std::vector<std::byte>> (by clean way I mean not looping on the string and appending each char).
What is the best way to do that ?
Side question: What is the best way to read a string from a std::vector<std::byte>> ?
Just use the ranged-insert overload (#4):
void extend(std::vector<std::byte>& v, std::string const& s) {
auto bytes = reinterpret_cast<std::byte const*>(s.data());
v.insert(v.end(), bytes, bytes + s.size());
}
You can read char as byte, it's a permitted alias.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With