Alessandro Power Alessandro Power - 1 year ago 104
C++ Question

Divide a std::string without copying

Is it possible to divide a

into two or more substrings without copying, much like we can use a move constructor to create a new
without copying?

Answer Source

You can not do this with std::string. But, you can do this with std::string_view from C++17.


std::string str = "TheBigStr";
std::string_view p1 = std::string_view( + 3, 3);

String view is not copying the data, so str should not be modified while the view is used.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download