说我有这个:
void test(std::vector<int>& my_ints) {
for (auto& my_int : my_ints) {
if (my_int == 5 && /* not the last value in the vector */) {
my_int += /* next value in the vector */;
}
}
}
Run Code Online (Sandbox Code Playgroud)
有什么有效的语法可以替换注释吗?
PS!是的,我知道,有一个常规的for循环是小菜一碟,但我想看看我是否可以对这种类型的东西使用range-for循环。
是否可以窥视下一个元素
在一般情况下-不。
由于对象std::vector是连续存储的,因此可以这样做*(&my_int + 1),但是如果以后更改容器,则代码可能会静默中断。不要那样做!
要检查当前元素是否为最后一个元素,可以使用&my_int == &my_ints.back()。
| 归档时间: |
|
| 查看次数: |
107 次 |
| 最近记录: |