c++ - 在 vector 的 vector 中找到 Point3f 的最大值

标签 c++ opencv c++11 vector

我看到有点类似的问题here但我没有得到我要找的东西。我有这样的东西

vector< vector<Point3f> > 3dpoints;

现在假设我只想找到 x 坐标的最大值,并想打印与其关联的所有其他值。我试过如下,但它抛出一些错误request for member 'begin' ...

for( auto r = 0; r < 3dpoints.size(); r++ ) {
    for( auto s = 0; s < 3dpoints[r].size(); s++ ) {
        cout<< max_element( 3dpoints[r][s].x.begin(), 3dpoints[r][s].x.end() ) << endl; 
    } 
}

我知道我遗漏了一些基本的东西,但无法得到它。谁能帮我找到 Point3f 中的最大值?

最佳答案

template<class F>
struct projected_order_t {
  F f;
  template<class Lhs, class Rhs>
  bool operator()(Lhs const& lhs, Rhs const& rhs)const {
    return f(lhs) < f(rhs);
  }
};
template<class F>
projected_order_t<F> projected_order(F f) {
  return {std::move(f)};
}

auto max_elem_of_vector = [](std::vector<Point3f> const& pts){
  return std::max_element( pts.begin(), pts.end(), projected_order(
    [](Point3f pt){ return pt.x; }
  ));
};
auto max_x_of_vector = [](std::vector<Point3f> const& pts){
  auto it = max_elem_of_vector(pts);
  if (it == pts.end()) return std::numeric_limits<float>::min();
  return it->x;
};
auto max_elem_of_v_of_v = [](std::vector<std::vector<Point3f>> const& pts){
  auto it = std::max_element( pts.begin(), pts.end(), projected_order(
    max_x_of_vector
  ));
  auto minf = std::numeric_limits<float>::min();
  auto minp = Point3f{minf, minf, minf};
  if (it == pts.end())
    return minp
  auto it2 = max_elem_of_vector(*it);
  if (it2 == it->end()) 
    return minp;
  return *it2;
};

max_elem_of_v_of_v应该可以解决您的问题。

投影顺序采用投影(从类型 A 到类型 B 的映射),并返回使用到 B 和 < 的映射的类型 A 的排序。在 B .

第一次使用将一个点映射到它的 x协调;这让我们可以通过 x 找到点 vector 中的最大元素坐标。

第二次使用将点 vector 映射到该 vector 中任何元素的最大 x。我们用它来找到具有最大 x 元素的 vector 。

然后我们从具有最大 x 元素的最大 vector 中提取元素。

如果没有最小元素,它返回最小浮点值。

关于c++ - 在 vector 的 vector 中找到 Point3f 的最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43939536/

相关文章:

c++ - 使用 Qt5 进行离屏渲染(openGL)

c++ - C/C++ 中固定长度实数输入数据的高效二维 FFT

c++ - OpenCV 矩阵运算是否比简单的 for 循环迭代更快?

c - 为什么 getopt_long 会忽略一些命令行选项

c# - 对于良好的编码实践,如果我们已经在方法 1 中验证并且方法 1 将该数据传递给方法 2,我们是否仍然需要在方法 2 中再次验证数据?

c++ - 在复制构造函数中调用赋值运算符

c++ - 控制台崩溃输出指针数组和迭代 C++

python - 相机捕获蓝色时如何打印文本?

c++ - 继承构造函数是否适用于 C++0x 中的模板?

c++ - 从 ifstream 读取不会读取空格