我知道如何找到数据类型的最大值,如 int
、float
、double
等。但我在这里使用 Point3f
处理 x
、y
和 z
坐标。那么任何人都可以帮助从 std::vector
中找到 x
、y
或 z
的最大值std::vector
?
std::vector< std::vector<Point3f> > cluster_points;
为简单起见,假设我只想在所有 x
轴中找到最高值。
最佳答案
我不知道 opencv,所以可以使用更简单的解决方案但是...
有几个 std::for_each()
和几个 lambda 函数怎么样?
std::vector<std::vector<Point3f>> vvp { /* some data */ };
auto mx = vvp[0][0].x;
auto my = vvp[0][0].y;
auto mz = vvp[0][0].z;
std::for_each(vvp.cbegin(), vvp.cend(),
[&](std::vector<Point3f> const & vp)
{ std::for_each(vp.cbegin(), vp.cend(),
[&](Point3f const & p)
{ mx = std::max(mx, p.x);
my = std::max(my, p.y);
mz = std::max(mz, p.z); }); });
如果你可以使用 C++14,那么带有 auto
参数的 lambda 函数,双 std::for_each()
部分可以简单地写成
std::for_each(vvp.cbegin(), vvp.cend(), [&](auto const & vp)
{ std::for_each(vp.cbegin(), vp.cend(), [&](auto const & p)
{ mx = std::max(mx, p.x);
my = std::max(my, p.y);
mz = std::max(mz, p.z); }); });
所以无需显式 Poinf3f
并且可以被其他类似 point-3d 的类型使用。
关于c++ - vector< vector<Point3f>> 中的最高值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43896491/