方法std::basic_string::substr
具有参数 pos, count
,指定位置 [pos, pos + count + 1) 中元素的逻辑范围。相反,大多数标准库函数(例如,std::for_each
)都有参数,其形式类似于begin, end
,指定范围[begin, end)
这似乎是一个异常(exception),然后,让一些人感到困惑(参见问题 here 、 here 、 here 和 here )。为什么这里没有使用通常的范围约定?还要注意 std::vector::erase
,另一个随机访问容器的方法,确实遵循通常的约定。
最佳答案
历史原因。标准库有多个起源,STL 就是其中之一。它带来了 begin,end
约定。 std::string
早于 STL 合并到标准库中,并且有大量现有代码使用了 .substr(pos,length)
。
关于c++ - 为什么 std::basic_string::substr 不遵循 [begin, end) 约定?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39738980/