Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/161.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ STL算法处理符合条件的前N个元素_C++_Stl_C++17 - Fatal编程技术网

C++ STL算法处理符合条件的前N个元素

C++ STL算法处理符合条件的前N个元素,c++,stl,c++17,C++,Stl,C++17,我有一段代码,它迭代一个坐标向量。然后我想使用向量的前N个坐标,它符合一个条件。 方法(简化): 在stl中是否有任何函数可以做到这一点?(我确实检查过,只是想知道我是否遗漏了一些不太明显的内容)您可以使用std::all\u of并在要停止时从谓词返回false: std::all_of(coordinates.begin(), coordinates.end(), [&](const auto& coordinate) {

我有一段代码,它迭代一个坐标向量。然后我想使用向量的前N个坐标,它符合一个条件。 方法(简化):


在stl中是否有任何函数可以做到这一点?(我确实检查过,只是想知道我是否遗漏了一些不太明显的内容)

您可以使用
std::all\u of
并在要停止时从谓词返回
false

std::all_of(coordinates.begin(), coordinates.end(),
            [&](const auto& coordinate)
            {
               if (image.at(coordinate) == 255)
                  filtered.emplace_back(coordinate, ...);
               return filtered.size() < maxCount;
            });
std::所有(坐标.begin(),坐标.end(),
[&](常数自动和坐标)
{
如果(图像在(坐标)==255)
已过滤。向后放置(坐标,…);
返回过滤后的.size()

只是我不确定这是否比循环更具可读性。

大多数STL算法专注于一个特定的任务,它们的组合不好-这就是为什么我们首先会得到范围。在那之前你要么

  • 将您的意图拆分为多个算法调用
  • 为您的用例编写一个特定的算法
  • 继续坚持手动循环(毕竟它们可以非常可读)或
  • 使用一个可用的范围库

  • 建议2.具体算法:

    template <class InputIt, class Size, class OutputIt, class Pred, class Fct>
    auto transform_n_if(InputIt first, InputIt last, Size count, OutputIt dest,
        Pred pred, Fct transform)
    {
       Size n(0);
    
       while (first != last && n !=count) {
          if (pred(*first)) {
             *dest++ = transform(*first);
             ++n;
          }
    
          ++first;
       }
    
       return dest;
    }
    
    模板
    自动转换\u n\u如果(先输入,后输入,大小计数,输出目标,
    Pred Pred,Fct变换)
    {
    尺寸n(0);
    while(first!=last&&n!=计数){
    if(pred(*first)){
    *dest++=transform(*first);
    ++n;
    }
    ++第一,;
    }
    返回目的地;
    }
    
    可以这样调用:

    for_each_n_if(begin(v), N, [&](Point p){ 
     if(cond) {
          otherVec.emplace_back(p,...);
          return true; // help counter is incremented
     } else {return false;}});
    
    std::vector<Point> coordinates(someSize);
    std::vector<SomeClass> filtered;
    
    transform_n_if(coordinates.cbegin(), coordinates.cend(), 42, std::back_inserter(filtered),
          [](const Point& p){ return /* your condition here... */ true; },
          [](const Point& p){ return /* your transformation here... */ SomeClass(p); });
    
    std::向量坐标(某种尺寸);
    std::矢量滤波;
    转换\u n\u if(coordinates.cbegin(),coordinates.cend(),42,std::back\u插入器(已过滤),
    [](const Point&p){return/*此处的条件…*/true;},
    [](const Point&p){return/*此处的变换…*/SomeClass(p);});
    
    为什么?循环不可读吗?只要有可能就尝试使用stl。我认为可能有一些函数可以处理这种特殊情况这考虑
    count
    输入,而不是在
    count
    outputs@Caleth:通过仅在谓词为true时递增
    n
    即可轻松修复。(完成)我倾向于将谓词视为纯函数,因此这对我来说不太容易理解。
    [&]
    是一个快速的赠品,表明这不是纯粹的。@MSalters:如果允许编译器在后台使用并行策略,甚至不确定它是否有效。@Jarod42没有执行策略参数的
    std::all_的版本需要一个输入迭代器。那么它不能为随机迭代器分派吗?
    
    std::vector<Point> coordinates(someSize);
    std::vector<SomeClass> filtered;
    
    transform_n_if(coordinates.cbegin(), coordinates.cend(), 42, std::back_inserter(filtered),
          [](const Point& p){ return /* your condition here... */ true; },
          [](const Point& p){ return /* your transformation here... */ SomeClass(p); });