我正在记录一段代码,写道我们需要选择的容器是 ReversibleContainer
。
详细阅读 std::vector
的描述> 和 std::basic_string
:
- 对于
std::vector
在其成员类型部分(迭代器)下,我们分别阅读:
std::vector
(for T other thanbool
) meets the requirements ofContainer
,AllocatorAwareContainer
(since C++11),SequenceContainer
,ContiguousContainer
(since C++17) andReversibleContainer
.
LegacyRandomAccessIterator
andLegacyContiguousIterator
tovalue_type
- 对于
std::basic_string
在其成员类型部分,我们读到:
std::basic_string
satisfies the requirements ofAllocatorAwareContainer
(except that customized construct/destroy are not used for construction/destruction of elements),SequenceContainer
andContiguousContainer
(since C++17).
LegacyRandomAccessIterator
andLegacyContiguousIterator
tovalue_type
在我看来 std::basic_string
满足成为 ReversibleContainer 的所有要求与 std::vector
相同,但没有这样描述。
不是我的问题,但我也不明白为什么它没有被描述为 Container
与 std::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/