Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何实现级联相关对象的排序比较器?_C++_Algorithm_Sorting_Grouping_Comparator - Fatal编程技术网

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
  • 递归执行步骤3,直到未找到父id
  • 问题:这能以更简单的方式实现吗


    <强>注:这个问题不是针对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之间的比较。尽管如此,我已经找到了一个解决方案,并更新了答案。