在向量向量中找到 Point3f 的最大值
Find maximum for Point3f in vector of vector
我在这里看到了一个类似的问题,但我没有得到我想要的东西。我有这样的东西
vector< vector<Point3f> > 3dpoints;
现在假设我只想找到 x 坐标的最大值,并想打印与之关联的所有其他值。我像下面一样尝试,但它为成员"开始"抛出了一些错误请求......
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 的映射(,并返回类型 A 上的排序,该排序使用映射到 B 并在 B
上<
。
第一种用途将点映射到其x
坐标;这使我们能够通过x
坐标在点向量中找到max元素。
第二种用法将点向量映射到该向量中任何元素的最大 x。 我们用它来找到具有最大 x 元素的向量。
然后,我们从具有最大x元素的最大向量中提取元素。
如果没有 min 元素,则返回最小浮点值。
您可以通过单次传递来做到这一点:
vector< vector<Point3f> > points;
vector<Point3f> maxes;
for( const auto &v : points ) {
for( const auto &p : v ) {
if( not maxes.empty() and maxes.front().x < p.x )
maxes.clear();
if( maxes.empty() or maxes.front().x == p.x )
maxes.push_back( p );
}
}
// here you have list of all points with max x in maxes
这是一个展示这个想法的例子,在你的代码中,你可能想用使用 epsilon 的函数替换 <和>
显示的PS代码对所有数据都这样做,您提到您需要单独为每一行执行此操作。可以轻松更改代码以执行此操作:
for( const auto &v : points ) {
vector<Point3f> maxes;
for( const auto &p : v ) {
if( not maxes.empty() and maxes.front().x < p.x )
maxes.clear();
if( maxes.empty() or maxes.front().x == p.x )
maxes.push_back( p );
}
// print maxes here
}
和>根据你最新的评论,代码应该是:
for( auto r = 0; r < 3dpoints.size(); r++ ) {
auto highest = max_element(begin(3dpoints[r]), end(3dpoints[r]),
[](const Point3f &lhs, const Point3f &rhs)
{ return lhs.x < rhs.x; })
cout << highest->y << highest->z << endl;
}
你做错了什么:
- 3dpoints[r][s].x 是一个浮点数,没有 begin((/end((。
- 您需要为max_element提供自定义比较功能。
编辑感谢@Slava指出 std::max_element 返回一个迭代器。
相关文章:
- 以C++递归方式查找向量中的最大值
- C++ - 如何在结构向量中找到结构体一个成员的最大值?
- 我的 If Else 语句无法在向量 (C++) 中提供最大值
- 如何修复错误:使用向量查找最小值和最大值进行合并排序
- 求长向量的最小值和最大值
- 在二维向量的每一列中查找最大值
- 向量中的最大值
- 从向量中提取最小值、最大值和中位数的最有效方法是什么
- 在向量向量中找到 Point3f 的最大值
- SSE2 内部函数 - 找到两个无符号短向量的最大值
- 使用算法查找自定义数据向量中的最大值和最小值
- 找到两个向量数组的组合,该数组的总和是 c++ 中所有其他组合的最大值
- 如何在不使用min_element C++的情况下找到向量中的最小值和最大值<double>
- 获取向量<float>中 k 个最大值的 indize 的有效方法
- 将向量用于负最大值算法 c++ 的问题
- 求向量的奇数位置最大值(以C++为单位)
- 在对象向量中查找最大值
- 一个模板函数,用于获取向量中的最大值及其相应的索引
- 如何找到向量的统计量?最大值/最小值、模式、中位数、平均值
- 从向量向量中的列中获取最小值和最大值