C++ 如何实现级联相关对象的排序比较器?
假设必须对以下类型的连续数组进行排序:C++ 如何实现级联相关对象的排序比较器?,c++,algorithm,sorting,grouping,comparator,C++,Algorithm,Sorting,Grouping,Comparator,假设必须对以下类型的连续数组进行排序: struct X { string id, parent_id; int value; bool operator< (const X& x) const { return value < x.value; } }; 写入比较器以创建以下排序数组的最佳方法是什么: {i, p, v} ---------- {c, "", 3} // grouping of 'c' {dc, c, 4} {a, "", 1} //
struct X
{
string id, parent_id;
int value;
bool operator< (const X& x) const { return value < x.value; }
};
写入比较器以创建以下排序数组的最佳方法是什么:
{i, p, v}
----------
{c, "", 3} // grouping of 'c'
{dc, c, 4}
{a, "", 1} // grouping of 'a'
{ea, a, 5}
{b, "", 2} // grouping of 'b'
{fb, b, 6}
正如您所看到的,数组在父id创建分组的地方进行了特殊排序,然后根据从最低到最高的值排列数组。换句话说,最新的从属对象(那些具有非空的父id的X
)是关键角色。剩下的父母被拉向他们
我的努力:自然的方法是:
值
x
的parent_id
;如果有效,则:
- 搜索父id,复制并删除该元素
- 在x上方插入
x
<强>注:这个问题不是针对C++的。
< P>你当前的比较器可能看起来是这样的:int cmp = a.string_id.compare(b.string_id);
if(cmp == 0) {
cmp = a.parent_id.compare(b.parent_id);
if(cmp == 0) {
cmp = a.value - b.value;
}
}
return cmp < 0;
intcmp=a.string\u id.compare(b.string\u id);
如果(cmp==0){
cmp=a.父项id.比较(b.父项id);
如果(cmp==0){
cmp=a.值-b.值;
}
}
返回cmp<0;
您可以使用稍微不同的比较器来实现所需的排序:
auto a_effective_parent_id = a.parent_id.empty() ? a.string_id : a.parent_id;
auto b_effective_parent_id = b.parent_id.empty() ? b.string_id : b.parent_id;
// compare first by 'effective' parent id
int cmp = a_effective_parent_id.compare(b_effective_parent_id);
if(cmp == 0) {
// here we order items with empty parent_id before children
cmp = a.parend_id.compare(b.parent_id);
if(cmp == 0) {
// then compare by 'string_id'
cmp = a.string_id.compare(b.string_id);
if(cmp == 0) {
// and eventually compare by 'value'
cmp = a.value - b.value;
}
}
}
return cmp < 0;
auto a_effective_parent_id=a.parent_id.empty()?a、 字符串\u id:a.parent\u id;
自动b_生效\u父级\u id=b.parent\u id.empty()?b、 string_id:b.parent_id;
//首先按“有效”父id进行比较
int cmp=a\u有效\u父级\u id.比较(b\u有效\u父级\u id);
如果(cmp==0){
//在这里,我们在子项之前订购父项id为空的项
cmp=a.parend\u id.compare(b.parent\u id);
如果(cmp==0){
//然后通过“string\u id”进行比较
cmp=a.string\u id.compare(b.string\u id);
如果(cmp==0){
//并最终通过“价值”进行比较
cmp=a.值-b.值;
}
}
}
返回cmp<0;
这意味着我们首先按照parent\u id
进行排序,如果parent\u id
为空,我们就好像string\u id
是parent\u id
(我称之为有效的parent\u id
,在概念上与unix中的有效用户id类似:)
如果无法修改类comparator方法,可以实现特定的comparator(例如在lambda或functor中),并将其与std::sort
一起使用
一些参考资料:
- ,以使用lambda和函子进行排序为例
int cmp = a.string_id.compare(b.string_id);
if(cmp == 0) {
cmp = a.parent_id.compare(b.parent_id);
if(cmp == 0) {
cmp = a.value - b.value;
}
}
return cmp < 0;
intcmp=a.string\u id.compare(b.string\u id);
如果(cmp==0){
cmp=a.父项id.比较(b.父项id);
如果(cmp==0){
cmp=a.值-b.值;
}
}
返回cmp<0;
您可以使用稍微不同的比较器来实现所需的排序:
auto a_effective_parent_id = a.parent_id.empty() ? a.string_id : a.parent_id;
auto b_effective_parent_id = b.parent_id.empty() ? b.string_id : b.parent_id;
// compare first by 'effective' parent id
int cmp = a_effective_parent_id.compare(b_effective_parent_id);
if(cmp == 0) {
// here we order items with empty parent_id before children
cmp = a.parend_id.compare(b.parent_id);
if(cmp == 0) {
// then compare by 'string_id'
cmp = a.string_id.compare(b.string_id);
if(cmp == 0) {
// and eventually compare by 'value'
cmp = a.value - b.value;
}
}
}
return cmp < 0;
auto a_effective_parent_id=a.parent_id.empty()?a、 字符串\u id:a.parent\u id;
自动b_生效\u父级\u id=b.parent\u id.empty()?b、 string_id:b.parent_id;
//首先按“有效”父id进行比较
int cmp=a\u有效\u父级\u id.比较(b\u有效\u父级\u id);
如果(cmp==0){
//在这里,我们在子项之前订购父项id为空的项
cmp=a.parend\u id.compare(b.parent\u id);
如果(cmp==0){
//然后通过“string\u id”进行比较
cmp=a.string\u id.compare(b.string\u id);
如果(cmp==0){
//并最终通过“价值”进行比较
cmp=a.值-b.值;
}
}
}
返回cmp<0;
这意味着我们首先按照parent\u id
进行排序,如果parent\u id
为空,我们就好像string\u id
是parent\u id
(我称之为有效的parent\u id
,在概念上与unix中的有效用户id类似:)
如果无法修改类comparator方法,可以实现特定的comparator(例如在lambda或functor中),并将其与std::sort
一起使用
一些参考资料:
- ,以使用lambda和函子进行排序为例
结构X
中添加一个额外的int
。是的,每个对象的内存将增加4-8个字节,但这符合我的要求
struct X
{
string id, parent_id;
int value, value_group = 0;
// ^^^^^^^^^^^ new indicator to be used while sorting
bool operator< (const X& x) const
{ return (value_group == x.value_group)? value < x.value : value_group < x.value_group; }
void print () const { cout << "{" << id << ", " << parent_id << ", " << value << ", " << value_group << "}\n"; }
};
.这里的逻辑是在
结构X
中添加一个额外的int
。是的,每个对象的内存将增加4-8个字节,但这符合我的要求
struct X
{
string id, parent_id;
int value, value_group = 0;
// ^^^^^^^^^^^ new indicator to be used while sorting
bool operator< (const X& x) const
{ return (value_group == x.value_group)? value < x.value : value_group < x.value_group; }
void print () const { cout << "{" << id << ", " << parent_id << ", " << value << ", " << value_group << "}\n"; }
};
.谢谢你的回答。如果以一个工作示例的形式提到它,会更有帮助。此外,我无法获得
string
s之间的比较。尽管如此,我已经找到了一个解决方案,并更新了答案。谢谢你的回答。如果以一个工作示例的形式提到它,会更有帮助。此外,我无法获得string
s之间的比较。尽管如此,我已经找到了一个解决方案,并更新了答案。