本文介绍了在满足条件的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中查找最后一个元素
发布评论