为什么我的增量没有发生在最后一个元素上
Why my incrementation not happening to the last element?
这是我的代码,试图使用指针将元素增加1。我的最后一个元素没有增加。你能告诉我为什么吗
#include<iostream>
using namespace std;
void print_all( int *start, int *stop)
{
int *curr = start;
while(curr != stop)
{
++(*curr);
++curr;
cout<< "n"<<"content at address "<<curr<<" got incremented to "<< *curr<<"n";
}
}
int main()
{
int a[] = {3,4,5};
print_all(a, a+3);
return 0;
}
输出:
content at address 0x7ffe3ded3eb4 got incremented to 4
content at address 0x7ffe3ded3eb8 got incremented to 5
content at address 0x7ffe3ded3ebc got incremented to 0
让我们用循环的第一次迭代来解释这一点:
- 条件计算为
true
,执行主体 - 将
*curr
处的值增加一 - 将
curr
递增1以指向下一个数组成员 - 打印下一个数组成员第一个成员永远不会被打印
0
是未定义行为的实例;虽然仅指向数组之外的一个是定义良好的,但访问该地址的值是未定义的。
3rd步骤做得太早。将第3个rd与第4个交换,您就可以了。
相关文章:
- lower_bound()返回最后一个元素
- 使用运算符 [] 引用 std::vector 上最后一个元素时出现问题<>
- Lower_bound不适用于具有 3 个元素的向量的最后一个元素
- 仅显示链表的最后一个元素
- push_back通过自行创建的对象获取最后一个元素的向量
- 使用 map.end() 访问 map 的最后一个元素
- 如何知道地图中的最后一个元素是否被删除?
- std::矢量保存 只推送最后一个元素
- 反转后不返回最后一个元素
- 如何在新数组较小时创建新数组并将旧数组的最后一个元素复制到新数组中?
- 在 QListView 中显示最后一个元素
- C++:在进行切片时对迭代器的约定,特别是对于访问最后一个元素并最终将其删除
- 遍历列表包含排除最后一个元素的内容
- 如何打印第一个和最后一个元素的和,然后打印第二个和倒数第二个元素的总和,依此类推
- C++链表最后一个元素的迭代器?
- 这种获取模板参数包中最后一个元素的方法是否有隐藏的开销?
- 使用 STL 交换堆栈 C++ 中的第一个和最后一个元素
- 数组:如何在指定索引之前显示数组的第一个和最后一个元素以及数组元素的差异?
- 你能pop_back一个向量,并且仍然使用迭代器到最后一个元素吗?
- 如何使用第一个、中间和最后一个元素的中位数正确分区?