What is the correct C++ way of comparing a memory buffer with a constant string - strcmp(buf, "sometext")
? I want to avoid unnecessary memory copying as the result of creating temporary std::string objects.
Thanks.
If you're just checking for equality, you may be able to use std::equal
#include <algorithms>
const char* text = "sometext";
const int len = 8; // length of text
if (std::equal(text, text+len, buf)) ...
of course this will need additional logic if your buffer can be smaller than the text
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