C++ 向量中的最高值<;向量<;第3f点>&燃气轮机;
我知道如何找到数据类型的最高值,如C++ 向量中的最高值<;向量<;第3f点>&燃气轮机;,c++,opencv,c++11,vector,C++,Opencv,C++11,Vector,我知道如何找到数据类型的最高值,如int,float,double,等等。但在这里,我使用点3f来处理x,y,&z坐标。那么,有人能从std::vector的std::vector中找到x、y或z的最高值吗 std::vector< std::vector<Point3f> > cluster_points; std::vectorcluster_点; 为了简单起见,假设我只想在所有坐标轴中找到x的最高值。我不知道opencv,所以可以是更简单的解决方案,但是 两个s
int
,float
,double
,等等。但在这里,我使用点3f
来处理x
,y
,&z
坐标。那么,有人能从std::vector
的std::vector
中找到x
、y
或z
的最高值吗
std::vector< std::vector<Point3f> > cluster_points;
std::vectorcluster_点;
为了简单起见,假设我只想在所有坐标轴中找到
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); }); });
因此,无需明确说明Poinf3f和其他类似点3d的类型。我不知道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); }); });
因此,无需明确说明Poinf3f,可供其他类似点3d的类型使用。这是C++14
这是一个在客户端代码中没有显式循环的解决方案
template<class F>
auto foreacher(F&& f) {
return [f=std::forward<F>(f)](auto&& r)mutable{
for (auto&& e:decltype(r)(r))
f(decltype(e)(e));
};
}
std::vector<std::vector<Point3f>> data = {whatever};
auto mx = data[0][0].x;
auto task = foreacher(foreacher([&](Point3f const& e){
mx = (std::max)(mx, e.x);
}));
task(data);
模板
自动前置器(F&&F){
返回[f=std::forward(f)](自动和&r)可变{
用于(自动和e:decltype(r)(r))
f(decltype(e)(e));
};
}
std::vector data={whatever};
自动mx=数据[0][0].x;
自动任务=foreacher(foreacher([&](第3F点常量和e){
mx=(标准::最大值)(mx,e.x);
}));
任务(数据);
我们使用解决元素问题的lambda,然后将其包装在两个修饰符中,使其在参数的内容上迭代
这是C++14
这是一个在客户端代码中没有显式循环的解决方案
template<class F>
auto foreacher(F&& f) {
return [f=std::forward<F>(f)](auto&& r)mutable{
for (auto&& e:decltype(r)(r))
f(decltype(e)(e));
};
}
std::vector<std::vector<Point3f>> data = {whatever};
auto mx = data[0][0].x;
auto task = foreacher(foreacher([&](Point3f const& e){
mx = (std::max)(mx, e.x);
}));
task(data);
模板
自动前置器(F&&F){
返回[f=std::forward(f)](自动和&r)可变{
用于(自动和e:decltype(r)(r))
f(decltype(e)(e));
};
}
std::vector data={whatever};
自动mx=数据[0][0].x;
自动任务=foreacher(foreacher([&](第3F点常量和e){
mx=(标准::最大值)(mx,e.x);
}));
任务(数据);
我们使用解决元素问题的lambda,然后将其包装在两个修饰符中,使其在参数的内容上迭代
.您可以使用映射到此任务的
std::accumulate
:
const auto min = std::numeric_limits<float>::min();
Point3f init( min, min, min );
std::vector< std::vector<Point3f> > cluster_points;
auto max = std::accumulate( cluster_points.begin(), cluster_points.end(), init, []( const Point3f &p, const std::vector<Point3f> &v ) {
return std::accumulate( v.begin(), v.end(), p, []( const Point3f &p1, const Point3f &p2 ) {
return Point3f( std::max( p1.x, p2.x ), std::max( p1.y, p2.y ), std::max( p1.z, p2.z ) );
}
} ) );
const auto min=std::numeric_limits::min();
点3f init(min,min,min);
std::vectorcluster_点;
auto max=std::累加(cluster_points.begin()、cluster_points.end()、init、[](常数点3F&p、常数std::vector&v){
返回std::累计(v.begin(),v.end(),p,[](常量点3f和p1,常量点3f和p2){
返回点3f(std::max(p1.x,p2.x),std::max(p1.y,p2.y),std::max(p1.z,p2.z));
}
} ) );
这只需要C++11,使用C++14,可以通过在lambdas中使用
auto
参数来简化它您可以使用std::acculate
来很好地映射到此任务:
const auto min = std::numeric_limits<float>::min();
Point3f init( min, min, min );
std::vector< std::vector<Point3f> > cluster_points;
auto max = std::accumulate( cluster_points.begin(), cluster_points.end(), init, []( const Point3f &p, const std::vector<Point3f> &v ) {
return std::accumulate( v.begin(), v.end(), p, []( const Point3f &p1, const Point3f &p2 ) {
return Point3f( std::max( p1.x, p2.x ), std::max( p1.y, p2.y ), std::max( p1.z, p2.z ) );
}
} ) );
const auto min=std::numeric_limits::min();
点3f init(min,min,min);
std::vectorcluster_点;
auto max=std::累加(cluster_points.begin()、cluster_points.end()、init、[](常数点3F&p、常数std::vector&v){
返回std::累计(v.begin(),v.end(),p,[](常量点3f和p1,常量点3f和p2){
返回点3f(std::max(p1.x,p2.x),std::max(p1.y,p2.y),std::max(p1.z,p2.z));
}
} ) );
这只需要C++11,使用C++14,它可以通过在lambdas中使用
auto
参数来简化虽然有几个很好的答案,但我只是在写一个纯opencv,我把调查留给你最快的方法
std::vector<Point3f> points;
// .. fill the array
Mat pointsMat = Mat(points).reshape(1);
如果您希望对std::vector All_points
执行此操作,您可以使用列数=All_points.size()*3
制作一个单通道Mat
,并使用相同的函数minMaxLoc
。这将为所有点集提供minVal
和maxVal
您还可以获取minVal
和maxVal
的位置,如:
minMaxLoc(pointsMat, &minVal, &maxVal, &minLoc, &maxLoc);
这当然是在重塑垫
希望有帮助
p.S.C++11和C++14的答案令人赞叹虽然有几个很好的答案,但我只是在写一个纯opencv的答案,我把调查的最快方法留给你
std::vector<Point3f> points;
// .. fill the array
Mat pointsMat = Mat(points).reshape(1);
如果您希望对std::vector All_points
执行此操作,您可以使用列数=All_points.size()*3
制作一个单通道Mat
,并使用相同的函数minMaxLoc
。这将为所有点集提供minVal
和maxVal
您还可以获取minVal
和maxVal
的位置,如:
minMaxLoc(pointsMat, &minVal, &maxVal, &minLoc, &maxLoc);
这当然是在重塑垫
希望有帮助
p.S.对C++11和C++14的答案表示感谢您可以将
向量
转换为一个矩阵,并使用它来查找最小值和最大值。@RickM感谢您的建议。我之前考虑过这一点,但由于我在其他引用中使用了Point3f,所以我不想将其转换为Mat。如果我这样做,我必须在需要的时候将其反转。这会让它变慢,如果你喜欢的话,它不会变慢。这只是一个指向数据的指针。@RickM好的,我会看一下文档,然后再试一次。感谢您的建议为向量做任何事情,但提供一个自定义比较器,它只比较x坐标。您可以将向量
转换为一个矩阵,并使用它来查找最小值和最大值。@RickM感谢您的建议。我之前考虑过这一点,但由于我在其他引用中使用了Point3f,所以我不想将其转换为Mat。如果我这样做,我必须在需要的时候将其反转。这会让它变慢,如果你喜欢的话,它不会变慢。这只是一个指向数据的指针。@RickM好的,我会看一下文档,然后再试一次。谢谢苏