c++ - std::basic_string 是可逆容器吗?

标签 c++ language-lawyer stdstring

我正在记录一段代码,写道我们需要选择的容器是 ReversibleContainer
详细阅读 std::vector 的描述> 和 std::basic_string:

  • 对于 std::vector在其成员类型部分(迭代器)下,我们分别阅读:

std::vector (for T other than bool) meets the requirements of Container, AllocatorAwareContainer(since C++11), SequenceContainer, ContiguousContainer(since C++17) and ReversibleContainer.

LegacyRandomAccessIterator and LegacyContiguousIterator to value_type

std::basic_string satisfies the requirements of AllocatorAwareContainer (except that customized construct/destroy are not used for construction/destruction of elements), SequenceContainer and ContiguousContainer(since C++17).

LegacyRandomAccessIterator and LegacyContiguousIterator to value_type

在我看来 std::basic_string 满足成为 ReversibleContainer 的所有要求与 std::vector 相同,但没有这样描述。
不是我的问题,但我也不明白为什么它没有被描述为 Containerstd::vector 相同。

知道为什么会这样吗?

最佳答案

我在 reversible container中看到的唯一东西 std::basic_string 规范中未体现的要求是 rbegin/rend/crbegin /crend 成员函数缺乏恒定时间复杂度要求 ( [string.iterators] )。 std::basic_string 的成员函数没有这样的复杂性保证。

我不确定这是否是故意的,因为 std::basic_string 对其 size 成员函数 ( [string.capacity] ) 有恒定的时间复杂度要求。因此,std::basic_string 实现也应该始终能够在恒定时间内获取 rbegin。 @JanSchultke 已打开 LWG issue建议澄清 std::basic_string 的迭代器函数具有恒定的时间复杂度。

否则在我看来它们是可逆容器,并且上述 LWG 问题的拟议解决方案将澄清 std::basic_string 应该是一个可逆容器 .

关于c++ - std::basic_string 是可逆容器吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77677291/

相关文章:

c++ - 使用 realloc 增加大小与创建更大的动态数组

C 对转换指针的相等运算符

c++ - 编译时字符串赋值用于运行时识别

c++ - CUDA 执行时间

c++ - Tesseract:如何导出文本和边界框?

c++ - 同时使用 openGL 绘制两个对象

c++ - 为什么在 C++ 中允许多个预增量但在 C 中不允许?

c++ - 类模板的静态成员是否被隐式实例化?

c++ - 如何将整数字符串转换为二维整数 vector ?

c++ - 将 QString 转换为 std::string