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

xml*_*lmx 0 c++ string standards vector c++20

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

当调整到较小的大小时,向量容量永远不会减少,因为这会使所有迭代器无效,而不仅仅是那些会被等效pop_back()调用序列无效的迭代器。

但是,我无法找到从任何类似保证文档std::string::resize

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

Ser*_*eyA 5

标准中不存在这样的要求。见 21.3.3.2:

引用 basic_string 序列元素的引用、指针和迭代器可能会因该 basic_string 对象的以下使用而失效:

(4.1) 作为参数传递给任何以非常量 basic_string 为参数的引用的标准库函数。

(4.2) 调用非常量成员函数,operator[]、at、data、front、back、begin、rbegin、end、rend 除外。