std::string::resize(smaller_than_capacity) 是否保证现有迭代器仍然有效?

Does std::string::resize(smaller_than_capacity) guarantee existing iterators are still valid?

根据cpprefstd::vector::resize明确保证:

Vector capacity is never reduced when resizing to smaller size because that would invalidate all iterators, rather than only the ones that would be invalidated by the equivalent sequence of pop_back() calls.

但是,我在std::string::resizedocumentation中找不到任何类似的保证。

std::string::resize(smaller_than_capacity)是否保证现有的迭代器仍然有效?

标准中没有这样的要求。见 21.3.3.2:

References, pointers, and iterators referring to the elements of a basic_­string sequence may be invalidated by the following uses of that basic_­string object:

(4.1) Passing as an argument to any standard library function taking a reference to non-const basic_­string as an argument.

(4.2) Calling non-const member functions, except operator[], at, data, front, back, begin, rbegin, end, and rend.