【发布时间】:2019-11-01 11:20:55
【问题描述】:
我有一段代码遍历坐标向量。然后我想使用符合条件的向量中的前 N 个坐标。 方法(简化):
Matrix image;
int maxCount = N;
std::vector<Point> coordinates(sizeof >> maxCount);
std::vector<SomeClass> filtered;
filtered.reserve(maxCount);
for (const auto& coordinate : coordinates)
{
if (image.at(coordinate) != 255)
{
continue;
}
filtered.emplace_back(coordinate, ...);
// end when we have enought points
if (filtered.size() == maxCount)
{
break;
}
}
我想要的是避免 for 循环并使用 STL 库。大致是这样的:
for_each_n_if(begin(v), N, [&](Point p){
if(cond) {
otherVec.emplace_back(p,...);
return true; // help counter is incremented
} else {return false;}});
在 stl 中是否有任何功能可以做到这一点? (我确实检查过,只是想知道我是否遗漏了一些不太明显的东西)
【问题讨论】:
-
为什么?循环不可读吗?
-
尽可能地尝试使用 stl。我认为可能有一些功能可以处理这种特殊情况。