减少 begin() 迭代器,然后再次增加它
Decreasing begin() iterator and then increasing it again
这样的陈述按照标准有效吗?
std::string str{"123"};
auto it = str.begin();
--it;
++it; // Does *it point to character '1' now?
我已经在 g++ 4.7.2 和 clang++ 3.5 上尝试过这个 - *it
返回'1'
.这是 c++11 中的标准行为吗?
不,它无效。
这是未定义的行为,因为 24.2.6 [双向迭代器] 指出--it
的后置条件是结果必须是可取消引用的。正如示例中前面begin()
指出的那样,不满足此条件,因此代码是非法的。
由于不需要诊断,它似乎有效,但你不能(也不应该)依赖它。
相关文章:
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 数组索引的值没有增加
- 为什么我的代码在输出中增加了93天
- 如何声明特征矩阵,然后通过嵌套循环初始化它
- 有效地使用std::unordered_map来插入或增加键的值
- 这是我尝试让用户将值输入到数组中.然后将其隐藏为大量的星号
- boost::asio如何生成多个协同程序,然后加入它们
- 如何将图像传输到c++(dll)中的缓冲区,然后在c#的缓冲区中读/写
- 在std::thread中,joinable()然后join()线程安全吗
- C++ 动态数组每次添加时将大小增加 1 - 错误
- 为什么要增加导致崩溃的指针
- C++:如何读取分离变量,然后读取向量
- 为什么我的递归函数按降序打印,然后按升序打印?
- 如何增加几个月循环.然后显示
- C :从一定的时间戳启动计时器,然后将其增加
- 如何在大数组中最有效地增加指定范围内的值,然后找到最大值
- 如何分配结构,然后增加其大小
- 减少 begin() 迭代器,然后再次增加它
- 如何根据第一个元素对增加然后根据第二个元素减少对进行排序
- 内存消耗迅速增加,然后非常缓慢地下降;内存泄漏