sort array of struct

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tienlx

    #1

    sort array of struct

    Hi all,

    I want to sort an array of struct _line:

    ....
    typedef struct _line
    {
    int x1;
    int x2;
    } line;
    bool comp(line l1,line l2)
    {
    if (l1.x1==l2.x1) return (l1.x2 < l2.x2);
    return (l1.x1 < l2.x1);
    }

    line* R;
    ....
    R = new line[k];
    ....
    sort(&R[0],&R[k],comp);
    ....
    delete[] R;

    And when i printed out the array R, the result is wrong, but i run
    well when i use vector<line>. And i don't know why sometimes the
    program crashes when i use delete[] R; .Remove this line, it doesn't.
    I use DevC++ 5, Win Vista.


    Thanks for help,
    tl

  • ravinder thakur

    #2
    Re: sort array of struct

    On Oct 9, 7:04 pm, tienlx <tien...@gmail. comwrote:
    Hi all,
    >
    I want to sort an array of struct _line:
    >
    ...
    typedef struct _line
    {
    int x1;
    int x2;} line;
    >
    bool comp(line l1,line l2)
    {
    if (l1.x1==l2.x1) return (l1.x2 < l2.x2);
    return (l1.x1 < l2.x1);
    >
    }
    >
    line* R;
    ...
    R = new line[k];
    ...
    sort(&R[0],&R[k],comp);
    ...
    delete[] R;
    >
    And when i printed out the array R, the result is wrong, but i run
    well when i use vector<line>. And i don't know why sometimes the
    program crashes when i use delete[] R; .Remove this line, it doesn't.
    I use DevC++ 5, Win Vista.
    >
    Thanks for help,
    tl
    can you please paste what input (a small one) you are giving and what
    output you are expecting ? your comparator function might be doing the
    tricks.

    Comment

    • tienlx

      #3
      Re: sort array of struct

      Thanks for your reply,
      I used a loop to print out the array:
      for(int i=0;i<k;i++)
      {
      printf("%d ,%d\n",(R[i]).x1,(R[i]).x2);
      }

      Here is my output:
      3094712 ,3084072
      541872467 ,1987208531
      544039282 ,1701603654
      544039282 ,1701603654
      544499311 ,1970497878
      809000050 ,1869567068
      842231141 ,1547322171
      962359909 ,1413766192
      977484654 ,1986348124
      995913326 ,1348221507
      .....

      and what i want is
      1,1
      1,2
      1,3
      1,4
      ....
      1,10
      2,1
      2,2
      2,3
      ....
      2,10
      ....
      10,10


      As you see, my input was in range 1-10,1-10 and i have no idea why
      the result genenates random numbers.
      Also, why the program crashes when i put this line in the end of
      function to release the memory of array R:
      delete[] R;
      if i remove that line, i doesn't crash.

      thanks,
      tl

      On Oct 9, 9:28 pm, ravinder thakur <ravindertha... @gmail.comwrote :
      On Oct 9, 7:04 pm, tienlx <tien...@gmail. comwrote:
      >
      >
      >
      Hi all,
      >
      I want to sort an array of struct _line:
      >
      ...
      typedef struct _line
      {
      int x1;
      int x2;} line;
      >
      bool comp(line l1,line l2)
      {
      if (l1.x1==l2.x1) return (l1.x2 < l2.x2);
      return (l1.x1 < l2.x1);
      >
      }
      >
      line* R;
      ...
      R = new line[k];
      ...
      sort(&R[0],&R[k],comp);
      ...
      delete[] R;
      >
      And when i printed out the array R, the result is wrong, but i run
      well when i use vector<line>. And i don't know why sometimes the
      program crashes when i use delete[] R; .Remove this line, it doesn't.
      I use DevC++ 5, Win Vista.
      >
      Thanks for help,
      tl
      >
      can you please paste what input (a small one) you are giving and what
      output you are expecting ? your comparator function might be doing the
      tricks.

      Comment

      • Jim Langston

        #4
        Re: sort array of struct

        "tienlx" <tienlex@gmail. comwrote in message
        news:1191948309 .296192.298090@ g4g2000hsf.goog legroups.com...
        Thanks for your reply,
        I used a loop to print out the array:
        for(int i=0;i<k;i++)
        {
        printf("%d ,%d\n",(R[i]).x1,(R[i]).x2);
        }
        >
        Here is my output:
        3094712 ,3084072
        541872467 ,1987208531
        544039282 ,1701603654
        544039282 ,1701603654
        544499311 ,1970497878
        809000050 ,1869567068
        842231141 ,1547322171
        962359909 ,1413766192
        977484654 ,1986348124
        995913326 ,1348221507
        ....
        >
        and what i want is
        1,1
        1,2
        1,3
        1,4
        ...
        1,10
        2,1
        2,2
        2,3
        ...
        2,10
        ...
        10,10
        >
        >
        As you see, my input was in range 1-10,1-10 and i have no idea why
        the result genenates random numbers.
        Also, why the program crashes when i put this line in the end of
        function to release the memory of array R:
        delete[] R;
        if i remove that line, i doesn't crash.
        >
        thanks,
        tl
        >
        On Oct 9, 9:28 pm, ravinder thakur <ravindertha... @gmail.comwrote :
        >On Oct 9, 7:04 pm, tienlx <tien...@gmail. comwrote:
        >>
        >>
        >>
        Hi all,
        >>
        I want to sort an array of struct _line:
        >>
        ...
        typedef struct _line
        {
        int x1;
        int x2;} line;
        >>
        bool comp(line l1,line l2)
        {
        if (l1.x1==l2.x1) return (l1.x2 < l2.x2);
        return (l1.x1 < l2.x1);
        >>
        }
        >>
        line* R;
        ...
        R = new line[k];
        ...
        sort(&R[0],&R[k],comp);
        ...
        delete[] R;
        >>
        And when i printed out the array R, the result is wrong, but i run
        well when i use vector<line>. And i don't know why sometimes the
        program crashes when i use delete[] R; .Remove this line, it doesn't.
        I use DevC++ 5, Win Vista.
        >>
        Thanks for help,
        tl
        >>
        >can you please paste what input (a small one) you are giving and what
        >output you are expecting ? your comparator function might be doing the
        >tricks.
        All I can tell you is that the following produces the expected output. I.E.
        1,1
        1,2
        1,3
        etc...

        Paste your actual code that is producing the errornous output etc...

        #include <iostream>
        #include <algorithm>

        typedef struct _line
        {
        int x1;
        int x2;
        } line;

        bool comp(line l1,line l2)
        {
        if (l1.x1==l2.x1) return (l1.x2 < l2.x2);
        return (l1.x1 < l2.x1);
        }

        int main()
        {
        line* R;

        int row = 10;
        int col = 10;
        int k = row * col;

        R = new line[k];
        int count = 0;
        for ( int i = 10; i 0; --i )
        {
        for ( int j = 10; j 0; --j )
        {
        R[count].x1 = i;
        R[count].x2 = j;
        count++;
        if ( count k )
        {
        std::cout << "Overflowed the buffer, dummy!\n";
        return 0;
        }

        }
        }

        std::sort(&R[0],&R[k],comp);

        for(int i=0;i<k;i++)
        {
        printf("%d ,%d\n",(R[i]).x1,(R[i]).x2);
        }

        delete[] R;

        }


        Comment

        • terminator

          #5
          Re: sort array of struct

          On Oct 10, 10:16 am, "Jim Langston" <tazmas...@rock etmail.comwrote :
          "tienlx" <tien...@gmail. comwrote in message
          >
          news:1191948309 .296192.298090@ g4g2000hsf.goog legroups.com...
          >
          >
          >
          >
          >
          Thanks for your reply,
          I used a loop to print out the array:
          for(int i=0;i<k;i++)
          {
          printf("%d ,%d\n",(R[i]).x1,(R[i]).x2);
          }
          >
          Here is my output:
          3094712 ,3084072
          541872467 ,1987208531
          544039282 ,1701603654
          544039282 ,1701603654
          544499311 ,1970497878
          809000050 ,1869567068
          842231141 ,1547322171
          962359909 ,1413766192
          977484654 ,1986348124
          995913326 ,1348221507
          ....
          >
          and what i want is
          1,1
          1,2
          1,3
          1,4
          ...
          1,10
          2,1
          2,2
          2,3
          ...
          2,10
          ...
          10,10
          >
          As you see, my input was in range 1-10,1-10 and i have no idea why
          the result genenates random numbers.
          Also, why the program crashes when i put this line in the end of
          function to release the memory of array R:
          delete[] R;
          if i remove that line, i doesn't crash.
          >
          thanks,
          tl
          >
          On Oct 9, 9:28 pm, ravinder thakur <ravindertha... @gmail.comwrote :
          On Oct 9, 7:04 pm, tienlx <tien...@gmail. comwrote:
          >
          Hi all,
          >
          I want to sort an array of struct _line:
          >
          ...
          typedef struct _line
          {
          int x1;
          int x2;} line;
          >
          bool comp(line l1,line l2)
          {
          if (l1.x1==l2.x1) return (l1.x2 < l2.x2);
          return (l1.x1 < l2.x1);
          >
          }
          >
          line* R;
          ...
          R = new line[k];
          ...
          sort(&R[0],&R[k],comp);
          ...
          delete[] R;
          >
          And when i printed out the array R, the result is wrong, but i run
          well when i use vector<line>. And i don't know why sometimes the
          program crashes when i use delete[] R; .Remove this line, it doesn't.
          I use DevC++ 5, Win Vista.
          >
          Thanks for help,
          tl
          >
          can you please paste what input (a small one) you are giving and what
          output you are expecting ? your comparator function might be doing the
          tricks.
          >
          All I can tell you is that the following produces the expected output. I.E.
          1,1
          1,2
          1,3
          etc...
          >
          Paste your actual code that is producing the errornous output etc...
          >
          #include <iostream>
          #include <algorithm>
          >
          typedef struct _line
          {
          int x1;
          int x2;
          >
          } line;
          >
          bool comp(line l1,line l2)
          {
          if (l1.x1==l2.x1) return (l1.x2 < l2.x2);
          return (l1.x1 < l2.x1);
          >
          }
          >
          int main()
          {
          line* R;
          >
          int row = 10;
          int col = 10;
          int k = row * col;
          >
          R = new line[k];
          int count = 0;
          for ( int i = 10; i 0; --i )
          {
          for ( int j = 10; j 0; --j )
          {
          R[count].x1 = i;
          R[count].x2 = j;
          count++;
          if ( count k )
          {
          std::cout << "Overflowed the buffer, dummy!\n";
          return 0;
          }
          >
          }
          }
          >
          std::sort(&R[0],&R[k],comp);
          >
          for(int i=0;i<k;i++)
          {
          printf("%d ,%d\n",(R[i]).x1,(R[i]).x2);
          }
          >
          delete[] R;
          >
          >
          >
          }
          I think the problem is that he 'print's after 'delete'.
          printf("%d ,%d\n",(R[i]).x1,(R[i]).x2);
          why not to use 'cout'?

          #include <iostream>
          using namespace std;
          ....
          cout << R[i].x1 << ',' << (R[i].x2 <<'\n';

          regards,
          FM.

          Comment

          • Jim Langston

            #6
            Re: sort array of struct

            "terminator " <farid.mehrabi@ gmail.comwrote in message
            news:1192018931 .306495.109520@ o3g2000hsb.goog legroups.com...
            On Oct 10, 10:16 am, "Jim Langston" <tazmas...@rock etmail.comwrote :
            >"tienlx" <tien...@gmail. comwrote in message
            >>
            >news:119194830 9.296192.298090 @g4g2000hsf.goo glegroups.com.. .
            >>
            >>
            >>
            >>
            >>
            Thanks for your reply,
            I used a loop to print out the array:
            for(int i=0;i<k;i++)
            {
            printf("%d ,%d\n",(R[i]).x1,(R[i]).x2);
            }
            >>
            Here is my output:
            3094712 ,3084072
            541872467 ,1987208531
            544039282 ,1701603654
            544039282 ,1701603654
            544499311 ,1970497878
            809000050 ,1869567068
            842231141 ,1547322171
            962359909 ,1413766192
            977484654 ,1986348124
            995913326 ,1348221507
            ....
            >>
            and what i want is
            1,1
            1,2
            1,3
            1,4
            ...
            1,10
            2,1
            2,2
            2,3
            ...
            2,10
            ...
            10,10
            >>
            As you see, my input was in range 1-10,1-10 and i have no idea why
            the result genenates random numbers.
            Also, why the program crashes when i put this line in the end of
            function to release the memory of array R:
            delete[] R;
            if i remove that line, i doesn't crash.
            >>
            thanks,
            tl
            >>
            On Oct 9, 9:28 pm, ravinder thakur <ravindertha... @gmail.comwrote :
            >On Oct 9, 7:04 pm, tienlx <tien...@gmail. comwrote:
            >>
            Hi all,
            >>
            I want to sort an array of struct _line:
            >>
            ...
            typedef struct _line
            {
            int x1;
            int x2;} line;
            >>
            bool comp(line l1,line l2)
            {
            if (l1.x1==l2.x1) return (l1.x2 < l2.x2);
            return (l1.x1 < l2.x1);
            >>
            }
            >>
            line* R;
            ...
            R = new line[k];
            ...
            sort(&R[0],&R[k],comp);
            ...
            delete[] R;
            >>
            And when i printed out the array R, the result is wrong, but i run
            well when i use vector<line>. And i don't know why sometimes the
            program crashes when i use delete[] R; .Remove this line, it
            doesn't.
            I use DevC++ 5, Win Vista.
            >>
            Thanks for help,
            tl
            >>
            >can you please paste what input (a small one) you are giving and what
            >output you are expecting ? your comparator function might be doing the
            >tricks.
            >>
            >All I can tell you is that the following produces the expected output.
            >I.E.
            >1,1
            >1,2
            >1,3
            >etc...
            >>
            >Paste your actual code that is producing the errornous output etc...
            >>
            >#include <iostream>
            >#include <algorithm>
            >>
            >typedef struct _line
            >{
            > int x1;
            > int x2;
            >>
            >} line;
            >>
            >bool comp(line l1,line l2)
            >{
            > if (l1.x1==l2.x1) return (l1.x2 < l2.x2);
            > return (l1.x1 < l2.x1);
            >>
            >}
            >>
            >int main()
            >{
            > line* R;
            >>
            > int row = 10;
            > int col = 10;
            > int k = row * col;
            >>
            > R = new line[k];
            > int count = 0;
            > for ( int i = 10; i 0; --i )
            > {
            > for ( int j = 10; j 0; --j )
            > {
            > R[count].x1 = i;
            > R[count].x2 = j;
            > count++;
            > if ( count k )
            > {
            > std::cout << "Overflowed the buffer, dummy!\n";
            > return 0;
            > }
            >>
            > }
            > }
            >>
            > std::sort(&R[0],&R[k],comp);
            >>
            > for(int i=0;i<k;i++)
            > {
            > printf("%d ,%d\n",(R[i]).x1,(R[i]).x2);
            > }
            >>
            > delete[] R;
            >>
            >>
            >>
            >}
            >
            I think the problem is that he 'print's after 'delete'.
            >
            printf("%d ,%d\n",(R[i]).x1,(R[i]).x2);
            why not to use 'cout'?
            >
            #include <iostream>
            using namespace std;
            ...
            cout << R[i].x1 << ',' << (R[i].x2 <<'\n';
            Yes, when I orignally copied the code and did a quick program, I was
            printfing after the delete[] and getting the same results at the OP. So I
            moved the delete to the correct position. That may be possible.

            And actually there are a few things I would change about this program, get
            rid of the typedef (not needed in C++), std::cout instead of printf, etc..
            but I left everything as the OP had it to show that the code he had shown
            wasn't the issue.


            Comment

            Working...