在满足条件的std :: vector中查找最后一个元素

编程入门 行业动态 更新时间:2024-10-28 00:16:57
本文介绍了在满足条件的std :: vector中查找最后一个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有这个要求来找到向量中小于值的最后一个元素。

I have this requirement to find the last element in the vector which is smaller than a value.

就像find_first_of一样,但是我要倒数第一。我搜索了,发现没有find_last_of,但是有find_first_of。

Like find_first_of but instead of first i want last. I searched and found that there is no find_last_of but there is find_first_of.

为什么会这样?标准方法是将find_first_of与反向迭代器一起使用吗?

Why is that so? Is the standard way is to use find_first_of with reverse iterators?

推荐答案

使用反向迭代器,例如:

#include <iostream> #include <vector> int main() { std::vector<int> v{1,2,42,42,63}; auto result = std::find_if(v.rbegin(), v.rend(), [](int i) { return i == 42; }); std::cout << std::distance(result, v.rend()) << '\n'; }

实时演示。

更多推荐

在满足条件的std :: vector中查找最后一个元素

本文发布于:2023-10-27 16:20:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1533907.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:元素   条件   std   vector

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!