gpt4 book ai didi

c++ - std::string(itr, itr) 应该做什么?

转载 作者:太空宇宙 更新时间:2023-11-04 14:55:47 26 4
gpt4 key购买 nike

web site cplusplus documentation对于部分采用两个输入迭代器状态的 std::string 构造函数:

Copies the sequence of characters in the range [first,last), in the same order.

first, last:Input iterators to the initial and final positions in a range. The range used is [first,last), which includes all the characters between first and last, including the character pointed by first but not the character pointed by last.

first == last 的退化情况下,这意味着什么?一方面包含 first 而另一方面 last 被排除?官方 C++ 标准说在这种情况下应该发生什么?应该抛出异常吗?

最佳答案

我不知道您正在阅读的是什么文档,但标准说 (§21.4.2/15):

[..] constructs a string from the values in the range [begin, end), as indicated in the Sequence Requirements table

并且序列要求表(表 100)将有效范围 [i, j)X a(i, j) 定义为:

Constructs a sequence container equal to the range [i, j)

当第二个迭代器可以从第一个迭代器到达(通过递增)时,范围有效。对于两个相等的迭代器,范围为空。见§24.2.1/7:

A range is a pair of iterators that designate the beginning and end of the computation. A range [i,i) is an empty range; in general, a range [i,j) refers to the elements in the data structure starting with the element pointed to by i and up to but not including the element pointed to by j. Range [i,j) is valid if and only if j is reachable from i. The result of the application of functions in the library to invalid ranges is undefined.

因此,如果 first == last,如您所说,您将得到一个空字符串。如果 last 无法从 first 到达,则您有未定义的行为。

关于c++ - std::string(itr, itr) 应该做什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15142603/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com