gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-27 23:54:04 25 4
gpt4 key购买 nike

我看到有点类似的问题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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com