问题描述
我要填写一些结构,同时发现的最小元素。 PL在下面找到
I want to fill some structure while finding minimum element. Pl find the code below
tyoedef struct Point
{
double x, y;
}Point;
我有个的载体 -
的std ::矢量<点和GT; V
中,我有几个千分点。
I have a vector of points
- std::vector<Point> V
in which i have few thousand points.
有另一种结构我有
typedef struct cart
{
Point pt;
double val_1; // computed using only Pt
double val_2; // computer using only Pt
}cart;
现在我有两个任务:
- 我需要找到结构V最小元素。
-
填充结构的车,这是直接与V。
- I need to find minimum element from structure V.
Fill the structure cart, which is directly dependent on V.
我可以做到这一点使用以下code。
I can do this using following code.
std::vector<cart> vCart;
for(unsigned i = 0; i < V.size(); ++i)
{
cart thsElement;
thsElement.pt = V[i];
thsElement.val_1 = compute_val_1(V[i]);
thsElement.val_2 = compute_val_2(V[i]);
vCart.push_back(thsElement)
}
auto it = std::min_element(vCart.begin(), vCart.end(), lex_sort);
bool lex_sort(cart const &a, cart const &b)
{
if(a.pt.x < b.pt.x) return true;
if(a.pt.x == b.pt.x) return (a.pt.y < b.pt.y);
}
现在有一个明显的问题,这个实现。
Now there is an evident problem with this implementation.
有两个环。其中填充的结构和其他寻找最小元素(的std :: min_element()
必须有一个循环遍历所有的值)。我争取几毫秒'改善。因此,这不是一个好的code。此外,这似乎让 C_style
There are two loops. One for filling the structure and other for finding the min element (std::min_element()
has to have a loop to iterate over all the values). I am fighting for few miliseconds' improvement. So this is not a good code. Moreover, this seems so C_style
于是我想出了以下code。
So I came up with following code.
std::vector<cart> vCart;
std::iterator <vCart> st_ite;
auto it = std::min_element(V.begin(), V.end(), boost::bind(FillStruct_LexSort, st_ite, _1, _2)); // V is a vector of Point
bool FillStruct_LexSort(std::insert_iterator< std::vector<Cart>> vcpInput, const Point &a, const Point &b)
{
Cart thsPt;
if(a.x() < b.x())
{
thsPt.pt = b;
thsPt.val_1 = compute_val_1(b);
thsPt.val_2 = compute_val_2(b);
(*vcpInput++) = (thsPt);
return true;
}
if (a.x() == b.x())
{
if(a.y() < b.y())
{
thsPt.pt = b;
thsPt.val_1 = compute_val_1(b);
thsPt.val_2 = compute_val_2(b);
(*vcpInput++) = (thsPt);
return true;
}
}
thsPt.pt = a;
thsPt.val_1 = compute_val_1(b);
thsPt.val_2 = compute_val_2(b);
(*vcpInput++) = (thsPt);
return false;
}
现在的问题是 - 我得到分段错误。我不知道我应该如何使用迭代器插入值。我想提到过vCart,但vCart是调用min_element(..)之后空。我甚至尝试insert_iterator,但没有成功。
Now, the problem is - I get segmentation fault. I do not know how should I use iterator to insert a value. I tried passing reference to vCart, but vCart is empty after calling min_element(..). I even tried insert_iterator, but with no success.
所以PL建议。
推荐答案
看来你想要的东西,如:
It seems you want something like:
bool lex_sort(const Point& lhs, const Point& rhs)
{
return std::tie(lhs.x, lhs.y) < std::tie(rhs.x, rhs.y);
}
然后
auto it = std::min_element(V.begin(), V.end(), &lex_sort);
if (it == V.end()) {
// V is empty.
} else {
Cart thsPt;
thsPt.pt = it;
thsPt.val_1 = compute_val_1(*it);
thsPt.val_2 = compute_val_2(*it);
return thsPt;
}
请注意,如果 val_1
/ val_2
总是依赖 PT $ C $的C>,您可以添加
车该采取
点
Note that if val_1
/val_2
always depend of pt
, you can add a constructor for Cart
which take a Point
这篇关于填写结构,同时发现min_element,C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!