简体   繁体   English

获取 STL 向量中大于某个值的元素的所有位置

[英]Get all positions of elements in STL vector that are greater than a value

I would like to know how can I find the index positions of elements that verify a certain condition (for example greater than).我想知道如何找到验证某个条件(例如大于)的元素的索引位置。 For example if I have a vector of int values例如,如果我有一个 int 值向量

vector<int> V;

V contains the values 3 2 5 8 2 1 10 4 7 V 包含值3 2 5 8 2 1 10 4 7

and I want to get all the index positions of elements that are greater than 5. I know std::find_if but according to the documentation it just finds the first element that satisfies a condition.并且我想获取大于 5 的元素的所有索引位置。我知道std::find_if但根据文档,它只找到满足条件的第一个元素。

Loop std::find_if , starting from where you stopped last time. 循环std::find_if ,从上次停止的位置开始。

Sample ( see it work ): 样品( 见工作 ):

std::vector<size_t> results;

auto it = std::find_if(std::begin(v), std::end(v), [](int i){return i > 5;});
while (it != std::end(v)) {
   results.emplace_back(std::distance(std::begin(v), it));
   it = std::find_if(std::next(it), std::end(v), [](int i){return i > 5;});
}

First we set up the iterator with the first result. 首先,我们使用第一个结果设置迭代器。 If it's not found, the while loop never executes. 如果找不到,则while循环永远不会执行。 Otherwise, the index position is stored ( std::distance is basically a more generic it - std::begin(v) ), and the search continues onward. 否则,存储索引位置( std::distance基本上是更通用的it - std::begin(v) ),并继续搜索。

I think I'd use std::copy_if : 我想我会使用std::copy_if

std::vector<int> x{3, 2, 5, 8, 2, 1, 10, 4, 7};
std::vector<size_t> y(x.size());

std::iota(y.begin(), y.end(), 0);
std::copy_if(y.begin(), y.end(), 
             std::ostream_iterator<size_t>(std::cout, " "), 
             [&](size_t i) { return x[i] > 5; });

For me, this gives 3 6 8 , the indices of 8, 10 and 7 in x -- exactly what we want. 对我来说,这给出了3 6 8x ,8,10和7的指数 - 正是我们想要的。

If you're stuck with a C++98/03 compiler/library, you'll use std::remove_copy_if instead (and reverse the sense of the comparison). 如果您坚持使用C ++ 98/03编译器/库,那么您将使用std::remove_copy_if (并反转比较的意义)。 In this case, you obviously won't be able to use a lambda for the comparison either. 在这种情况下,您显然无法使用lambda进行比较。

Just for fun, transform_if algorithm: 只是为了好玩, transform_if算法:

#include <vector>
#include <iterator>
#include <algorithm>
#include <iostream>

template<typename InputIterator, typename OutputIterator,
    typename UnaryPredicate, typename UnaryFunction>
OutputIterator
transform_if (InputIterator first, InputIterator last,
    OutputIterator result, UnaryPredicate pred,
    UnaryFunction func)
{
    for (; first != last; ++first, ++result)
        if (pred(*first))
            *result = func(*first);
    return result;
}

int main()
{
    std::vector<int> x {3, 2, 5, 8, 2, 1, 10, 4, 7};
    std::vector<size_t> indices;

    size_t index = 0;
    transform_if(x.begin(), x.end(), std::back_inserter(indices),
        [&](int i){ return ++index, i > 5; },
        [&](int){ return index-1; });

    std::copy(indices.begin(), indices.end(),
              std::ostream_iterator<size_t>(std::cout, " "));
}

Output: 3 6 8 输出: 3 6 8

In C++20, with view, you might do在 C++20 中,使用视图,您可能会这样做

std::ranges::iota_view{0, (int)v.size()}
    | std::ranges::views::filter([&v](int i){ return v[i] > 5; })

Demo演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM