是std :: vector :: begin() - 1未定义? [英] Is std::vector::begin() - 1 undefined?

查看:227
本文介绍了是std :: vector :: begin() - 1未定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要以向后的顺序迭代一些元素并且我正在使用:

I need to iterate over some elements in backward order and I'm using:

for ( /* ... */ it = vec.end() - 1, end = vec.begin() ; it >= end ; --it ) {
    // ...

我现在 end() - 1 是为某些容器定义的,包括vector,但是现在我需要知道是否还定义了开始减量。

I now that end() - 1 is defined for some containers, including vector, but now I need to know if begin decrement is also defined.

我不知道我是否可以使用reverse_iterator,因为我需要将这些迭代器作为参数传递给std :: vector :: erase,并且从文档中看,它们看起来是不同的类型。

I don't know if I could use reverse_iterator, because I'll need to pass these iterators as parameters to std::vector::erase and from the documentation, it looks that they are different types.

推荐答案

是的,它是未定义的。

如果你想反向迭代元素,只需使用 rb egin 雷德 。它们是反向迭代器,专门为此目的而设计。如果需要从反向迭代器获取标准迭代器,可以使用 <$迭代器上的c $ c> base 成员函数

If you want to iterate over elements in reverse, just use rbegin and rend. They're reverse iterators, designed explicitly for this purpose. If you need to get a standard iterator from the reverse iterator, you can use the base member function on the iterator.

这篇关于是std :: vector :: begin() - 1未定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆