c++ - Iterator increment results 是一个新的迭代器吗?

标签 c++ pointers iterator

When we add(or subtract) an integral value to (or from) a pointer, the result is a new pointer. That new pointer points to the element the given number ahead of (or behind) the original pointer: (pp.119 c++ primer 5ed)

我也从书上了解到,指针是迭代器(pp.118 c++ primer 5ed)。

问题

我还可以声称迭代器上的算术运算创建了一个全新的迭代器吗?

最佳答案

这本书描述了一种情况,比如说,p + n,其中 p 是一个指针,n 是一个整数。该表达式产生一个指针类型的新值。由您决定将值存储在何处;您也可以决定根本不存储它。

递增指针,即写入 p += n,将原始指针的值更改为 p + n

它对迭代器的工作方式是相同的:it + n 产生一个新的迭代器,而 it += n 改变现有的迭代器。

注意 第一个表达式可以写成std::next(it, n) , 而第二个应该写成 std::advance(it, n) ,对于迭代器和指针。

关于c++ - Iterator increment results 是一个新的迭代器吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48608609/

相关文章:

C++ getline() 函数没有按预期工作

c++ - 如果我过早获得窗口句柄,控制台窗口会出现问题……?

c++ - 如何在 Visual C++ Windows 窗体应用程序中包含文件

c - malloc 的指针将自身重置为 NULL?

c - 在 C 中使用指针转置矩阵

c++ - 使用指针设置类属性时出错

c++ - 将基础实例指针转换为派生实例指针是否合法? (该实例不是派生实例)

c++ - 连续迭代器检测

python - tensorflow - TFRecordWriter 在写入文件时占用太多内存?

c++ - 迭代器和重载 << 运算符