如何使用另一个向量的排序顺序对结构化数据的向量进行排序 [英] How do I sort a vector of struced data with sortorder of another vector

查看:170
本文介绍了如何使用另一个向量的排序顺序对结构化数据的向量进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



不幸的是我现在坐在这个问题上4小时了,它让我抓狂:



i有2个向量我的班级成员。

firstvector包含结构<使用cstring名称,枚举种类和浮点转换因子>

和带有排序信息的向量由Enums填充Name(0),Kind(1)和transformation-factor (2)。

Enum在Vector中的位置告诉我们排序顺序。

我的目标是在数据网格中显示第一个矢量数据,排序由用户订购。



现在我有问题写一个工作函子,它会告诉std :: sort如何排序。



我尝试过的事情:



排序函数应该得到参数并应该停止堕胎很快,theres是第一个配对,什么不是l。 == r。,

现在我甚至无法在.header中获得仿函数的结构,

我也遇到了的问题调用.cpp中的函数,因为每一步都取决于排序向量。

谁可以帮助我并给我一个例子?

Hi,
unfortunately im sitting at this problem for 4hours now, and it drives me crazy:

i got an 2 vectors as a members of my class.
the firstvector contains structs < with cstring Name, enum Kind and float transformation-factor>
and the vector with the sorting informations is filled by Enums for "Name"(0), "Kind"(1) and "transformation-factor"(2).
The Position of the Enum in the Vector tells us the Sort Order.
My Goal is to display the first vector data in a data grid, sorted by the users order.

Now i have problems to write a working functor, which shall tell the std::sort how to sort.

What I have tried:

The sorting functions should get the parameters and should stop the sortion as soon theres is a first matching pair, what is NOT l. == r.,
right now i not even can get the struct for the functor down in the .header,
also i have problems with the call of the functions in the .cpp, cause every step depends on the sorting-vector.
Who can help me and give me an example?

推荐答案

要实现自己的排序顺序,必须覆盖比较运算符<来覆盖排序算法。在排序调用中重复调用它。



更深入的内部阅读并理解伟大的文章 STL排序比较功能



提示:制作一个小样本列表并调试它以理解发生了什么。
To achieve your own sort order you must overwrite the sorting algorithm, by overwriting the comparison operator "<" which is repeatly called in a sort call.

For a deeper inside read and understand the great article STL Sort Comparison Function.

Tip: make a small sample list and debug it to understand what is happening.


谢谢你的帮助。

i使用了STL Sort Comparision Funciton并且可以使用

会员<运营商

就像文章中描述的那样



谢谢你的帮助,

下次生病使用仿函数:)
thank you for your help.
i used the STL Sort Comparision Funciton and could use the
Member < Operator
like it was described in the Article

thank you for your help,
next time ill use a functor :)


这篇关于如何使用另一个向量的排序顺序对结构化数据的向量进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆