Yak*_*kov 7 c++ iterator vector
我需要获取一个迭代器到a中的最后一个元素std::vector.
已知end()方法返回一个迭代器,引用向量容器中的past-the-end元素.我可以通过使用实现我需要的东西end() - 1吗?
据我所知,我不能使用back()方法,因为它返回一个引用.
鉴于您知道/已检查向量是否为空并且C++ 11是一个选项,您可以使用std :: prev:
auto it = std::prev( your_vector.end() ); // or .cend() if a const_iterator is OK
Run Code Online (Sandbox Code Playgroud)
另请注意,using rbegin()返回一个反向迭代器,它与普通迭代器不同.