When shortening a std::string
there are basically two possibilities: string.resize(length)
or string.erase(length)
.
Given that we know a string will become smaller, what are the differences between the two?
resize
must check if length > current
or length < current
- it's behavior depends on result of сomparison. erase
always reduce length of stringresize
: Strong guarantee: if an exception is thrown, there are no changes in the string. ( from here )erase
: C++98 - equal to resize
; C++14 - there is no-throw version of function ( from here )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