[英]is it possible to iteration from .begin() to .rbegin()
I need to iterate from the beginning of a container to one element before the end. 我需要从容器的开头迭代到结尾之前的一个元素。 I can put an
if
condition inside the loop to bypass the last element but I was wondering if it would be possible to write the for loop like this: 我可以在循环中放置一个
if
条件,以绕过最后一个元素,但是我想知道是否可以像下面这样编写for循环:
for (it = C.begin(); it != C.rbegin(); it++){...}
if not, is there any suggestions? 如果没有,有什么建议吗?
Is it container dependent? 是否依赖于容器? (for now, I am using
std::vector
but it may change) (目前,我正在使用
std::vector
但可能会更改)
In addition to the comment by Tony D , you can use std::prev(C.end())
to get the iterator that precedes the end iterator: 除了Tony D的注释之外,您还可以使用
std::prev(C.end())
来获取位于结束迭代器之前的迭代器:
for (it = C.begin(); it != std::prev(C.end()); it++);
// ^^^^^^^^^^^^^^^^^^
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.