For example,
std::string_view strv{ "Hello" };
strv.remove_prefix(1);
The original string should be "Hello".
I tried using strv.data() and std::string str(strv.begin(), strv.end());
I can only get "ello" instead of "Hello".
I only found two methods right now.
use a variable(e.g. string str) to store original string then use use string_view. When you need the original string, use str.
use a variable(e.g. int removed_len) to record the length of removed prefixes. Then when you need the original string strv.data() - removed_len is the start place of the original string.
The "Hello" you use to construct strv is your original string. However, you never actually instantiate that string, making it a prvalue. If you want to keep a copy of your original string, try something like:
std::string str{"Hello"};
std::string_view strv{str};
strv.remove_prefix(1);
str will hold your original string.
strv.remove_prefix(1), why would you expect anything other than "ello"?data()member returns a pointer to the first element of the view, not to the first element of the original string. Same goes for the iterators. The reason is that a view is like a window open on an actual container, not the container itself.strv.data() - 1? but it is now outside of the view...strv.data() - 1?datapoints into an array to a position wheredata() - 1is still inside that same array. AFAIK the pointer arithmetic rules say that is okay.