void traceStringData(const std::string& s)
{ // print all character of the allocated memory:
std::print("[");
for (unsigned i = 0; i < s.capacity(); ++i) {
char c = s.data()[i];
if (c == '\0') c = 'T'; // use 'T' for the null terminator '\0'
if (!std::isprint(c)) c = '?'; // use '?' for non-printable characters
std::print("{}", c); // otherwise print the character as it is
} // show which characters are part of the value:
std::println("]\n\"{}\"\n", std::string(s.size(), '^'));
}
int main()
{ // simulate string with 25 characters, null terminator, and extra memory:
std::string s = "-------------------------T?????";
traceStringData(s); // -------------------------T?????
// resize string to have 10 characters:
s.resize(10);
traceStringData(s); // ----------T--------------T?????