[英]C++: Merge two vectors with tuples as element
I got two vectors which look like this: std::vector<std::tuple<unsigned int, unsigned int, float, Position, MemberOf>>
我有两个看起来像这样的std::vector<std::tuple<unsigned int, unsigned int, float, Position, MemberOf>>
: std::vector<std::tuple<unsigned int, unsigned int, float, Position, MemberOf>>
First I sorted them with首先我对它们进行了排序
std::sort(vector.begin(), vector.end(), sort_func());
struct sort_func {
inline bool operator() (std::tuple<unsigned int, float, Position> &element1, std::tuple<unsigned int, float, Position> &element2) {
if (std::get<1>(element1) < std::get<1>(element2) || (std::get<1>(element1) == std::get<1>(element2) && tau(element1) < tau(element2)))
return true;
else
return false;
}
unsigned int tau(std::tuple<unsigned int, float, Position> &element) {
if (std::get<2>(element) == end)
return 0;
else if (std::get<2>(element) == in_plane)
return 1;
else if (std::get<2>(element) == start)
return 2;
}
};
Now I got two sorted vectors and I want to merge them in the way, I sorted them.现在我得到了两个排序的向量,我想以这种方式合并它们,我对它们进行了排序。 I've seen, that there is the std::merge()
method.我已经看到,有std::merge()
方法。 But I am unsure how to use it in this complex way.但我不确定如何以这种复杂的方式使用它。
Can someone explain me how I can use my sort_func
when merging the two vectors?有人可以解释我如何在合并两个向量时使用sort_func
吗?
The final merged list should maintain the sort order.最终合并的列表应保持排序顺序。 That is the goal.这就是目标。
Thanks for your advice!谢谢你的建议!
You can use the same comparison function object in std::merge
see second version too.您也可以在std::merge
使用相同的比较函数对象,参见第二个版本。
// v3 - output vector, v1, v2 -> sorted vector
std::merge(
v1.begin(), v1.end(),
v2.begin(), v2.end(),
std::back_inserter(v3),
sort_func()
);
Like this:像这样:
std::vector<std::tuple<unsigned int, unsigned int, float, Position, MemberOf>> v1,v2,dst;
//fill v1,v2 however you did it
std::sort(v1.begin(), v1.end(), sort_func());
std::sort(v2.begin(), v2.end(), sort_func());
std::merge(v1.begin(), v1.end(), v2.begin(), v2.end(), std::back_inserter(dst),sort_func());
As mentioned here: http://en.cppreference.com/w/cpp/algorithm/merge The function to compare the two instances has to have the signature:如此处所述: http : //en.cppreference.com/w/cpp/algorithm/merge比较两个实例的函数必须具有签名:
bool cmp(const Type1 &a, const Type2 &b);
So you just have to implement it like that:所以你只需要像这样实现它:
bool cmp(const std::tuple<unsigned int, float, Position, MemberOf> &element1,
const std::tuple<unsigned int, float, Position, MemberOf> &element2);
There you can call your original function, if you want to.如果需要,您可以在那里调用原始函数。 After implementing it that way you should be able to use:以这种方式实施后,您应该能够使用:
std::vector<std::tuple<unsigned int, unsigned int, float, Position, MemberOf>> dest; // holds result
std::merge(v1.begin(), v1.end(), v2.begin(), v2.end(),
std::back_inserter(dest), cmp);
And it seems like the compare function parameters have not the correct type, as they are missing the MemberOf part mentioned in your first line.而且似乎比较函数参数的类型不正确,因为它们缺少第一行中提到的 MemberOf 部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.