std::sort

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

    std::sort

    say i had
    97456 and
    23456

    is there already a sort function to check which one begins with the smaller
    number rearrange it. in this case the bottom number should clearly be
    rearranged to the top as it lower in numerical order.
  • Unforgiven

    #2
    Re: std::sort

    JasBascom wrote:[color=blue]
    > say i had
    > 97456 and
    > 23456
    >
    > is there already a sort function to check which one begins with the
    > smaller number rearrange it. in this case the bottom number should
    > clearly be rearranged to the top as it lower in numerical order.[/color]

    Yes, it's called std::sort, as you already guessed:

    std::vector<int > v;
    v.push_back(974 56);
    v.push_back(234 56);
    std::sort(v.beg in(), v.end())

    You needn't specifically use a vector, it works with all containers that
    support random access iterators.

    --
    Unforgiven

    Comment

    • Jeff Flinn

      #3
      Re: std::sort


      "Unforgiven " <jaapd3000@hotm ail.com> wrote in message
      news:c1kpct$1i5 kih$1@ID-136341.news.uni-berlin.de...[color=blue]
      > JasBascom wrote:[color=green]
      > > say i had
      > > 97456 and
      > > 23456
      > >
      > > is there already a sort function to check which one begins with the
      > > smaller number rearrange it. in this case the bottom number should
      > > clearly be rearranged to the top as it lower in numerical order.[/color]
      >
      > Yes, it's called std::sort, as you already guessed:
      >
      > std::vector<int > v;
      > v.push_back(974 56);
      > v.push_back(234 56);
      > std::sort(v.beg in(), v.end())
      >
      > You needn't specifically use a vector, it works with all containers that
      > support random access iterators.[/color]

      Or ensure they're sorted to begin with using:

      std::set<int> s;

      s.insert(97456) ;
      s.insert(23456) ;

      int smallest = *s.begin();

      Jeff F



      Comment

      • JasBascom

        #4
        Re: std::sort

        sorry to bother you again.
        if i had
        union Allrecords{
        struct crecord Newcrecord;
        struct irrecord Newirrecord;
        struct drecord Newdrecord;
        }
        union Allrecords *rec;
        the structure element I want to sort now has to be accessed throw the union
        say
        rec.Newcrecord. customer_code; and
        rec.Newirrecord .customer_code and
        rec.Newdrecord. customer_code.

        this is being read in from a file, so I don't know what i would be pushing back
        (push_back)
        does it still work if I don't have a value to put in the brackets?
        instead of std::vector
        could I use std::rec?

        Comment

        • John Harrison

          #5
          Re: std::sort


          "JasBascom" <jasbascom@aol. com> wrote in message
          news:2004022607 5540.25780.0000 0461@mb-m01.aol.com...[color=blue]
          > sorry to bother you again.
          > if i had
          > union Allrecords{
          > struct crecord Newcrecord;
          > struct irrecord Newirrecord;
          > struct drecord Newdrecord;
          > }
          > union Allrecords *rec;
          > the structure element I want to sort now has to be accessed throw the[/color]
          union[color=blue]
          > say
          > rec.Newcrecord. customer_code; and
          > rec.Newirrecord .customer_code and
          > rec.Newdrecord. customer_code.
          >
          > this is being read in from a file, so I don't know what i would be pushing[/color]
          back[color=blue]
          > (push_back)
          > does it still work if I don't have a value to put in the brackets?
          > instead of std::vector
          > could I use std::rec?[/color]

          There's no such thing as std::rec. However you can use an array with
          std::sort (rec is a pointer to an array, right?). However you have to write
          a comparison functor to tell std::sort what to base the sort on. That's not
          too easy if you've never done one before, and I'm guessing here but I expect
          the point of the exercise is to write your own sort function?

          john


          Comment

          • John Harrison

            #6
            Re: std::sort


            "John Harrison" <john_andronicu s@hotmail.com> wrote in message
            news:c1kquk$1kj rd9$1@ID-196037.news.uni-berlin.de...[color=blue]
            >
            > "JasBascom" <jasbascom@aol. com> wrote in message
            > news:2004022607 5540.25780.0000 0461@mb-m01.aol.com...[color=green]
            > > sorry to bother you again.
            > > if i had
            > > union Allrecords{
            > > struct crecord Newcrecord;
            > > struct irrecord Newirrecord;
            > > struct drecord Newdrecord;
            > > }
            > > union Allrecords *rec;
            > > the structure element I want to sort now has to be accessed throw the[/color]
            > union[color=green]
            > > say
            > > rec.Newcrecord. customer_code; and
            > > rec.Newirrecord .customer_code and
            > > rec.Newdrecord. customer_code.
            > >[/color][/color]

            Another point which isn't clear from your post, is how you can tell which of
            the different records types you have in your array. How can you whether an
            Allrecords is a Newcrecord, a Newirecord or a Newdrecord? That is something
            you are going to have to work out whatever method of sorting you end up
            using.

            john


            Comment

            • Unforgiven

              #7
              Re: std::sort

              JasBascom wrote:[color=blue]
              > sorry to bother you again.
              > if i had
              > union Allrecords{
              > struct crecord Newcrecord;
              > struct irrecord Newirrecord;
              > struct drecord Newdrecord;
              > }
              > union Allrecords *rec;
              > the structure element I want to sort now has to be accessed throw the
              > union say
              > rec.Newcrecord. customer_code; and
              > rec.Newirrecord .customer_code and
              > rec.Newdrecord. customer_code.
              >
              > this is being read in from a file, so I don't know what i would be
              > pushing back (push_back)
              > does it still work if I don't have a value to put in the brackets?
              > instead of std::vector
              > could I use std::rec?[/color]

              You can sort any kind of user defined data type, you just need to make sure
              you define either an operator<() for that type or pass a BinaryPredicate as
              third parameter to std::sort.

              So you'd need to create something like this:
              bool operator<(const Allrecords &left, const Allrecord &right)
              {
              /* put any condition you want to sort on here */
              return left.Newcrecord .customer_code < right.Newcrecor d.customer_code ;
              }

              Then just use a std::vector<All records>.

              You can also sort any array, because a pointer behaves like an iterator and
              can be used as such. You could sort your rec, if it has num_elts elements,
              with std::sort(rec, rec + num_elts);

              --
              Unforgiven

              Comment

              • John Harrison

                #8
                Re: std::sort

                > However you have to write[color=blue]
                > a comparison functor to tell std::sort what to base the sort on.[/color]

                My mistake, Unforgiven is right, ignore the above.

                john


                Comment

                • JasBascom

                  #9
                  Re: std::sort

                  thank you unforgiven
                  is left an object of Allrecords
                  say: union Allrecords left, right?
                  I've never come across operators before, what is their purpose in the great
                  scheme of things?

                  std::vector<All records> is that to be declared before the operator function?

                  This is the sort function I have, how can i change it so it works in the manner
                  you suggested.

                  the union object rec
                  union Allrecords *rec
                  is being sorted from a binary file.
                  The idea behind this sort is to take two lines from the file and check that
                  they null terminate '\0' because customer_code is 6 arrays long. Then use
                  strcmp to compare the two, as Newcrecord.cust omer_code is declared as type
                  char, use temp to store it while shuffling the pack around.
                  As it is it doesn't work.


                  void sort_function( union Allrecords *rec, ifstream& validdata )
                  {

                  union Allrecords *str_ptr1 = rec;
                  union Allrecords *str_ptr2, tempstr;


                  for(int i =0; i< loop; i++)
                  while( strcmp(str_ptr1[i].Newcrecord.cus tomercode, '\0') ||
                  strcmp(str_ptr1[i].Newdrecord.cus tomercode, '\0') ||
                  strcmp(str_ptr1[i].Newirrecord.cu stomercode, '\0'))
                  {
                  str_ptr2 = str_ptr1 + 1;//set to next element.

                  for( i=0; i<loop; i++)
                  while( strcmp(str_ptr2[i].Newcrecord.cus tomercode, '\0') ||
                  strcmp(str_ptr2[i].Newdrecord.cus tomercode, '\0'))
                  {
                  for(int i=0; i<loop; i++)
                  if( strcmp( str_ptr1[i].Newirrecord.cu stomercode,
                  str_ptr2[i].Newirrecord.cu stomercode + 1))
                  {
                  tempstr = *str_ptr1;
                  *str_ptr1 = *str_ptr2;
                  *str_ptr2 = tempstr;

                  }
                  *str_ptr1++;//incremented, so that the same code isn't sorted again
                  }
                  str_ptr2++;
                  }

                  }

                  Comment

                  • Unforgiven

                    #10
                    Re: std::sort

                    JasBascom wrote:[color=blue]
                    > thank you unforgiven
                    > is left an object of Allrecords[/color]

                    Yes, so is right.
                    [color=blue]
                    > say: union Allrecords left, right?[/color]

                    You don't need to repeat the union keyword in C++. That's necessary in C,
                    but not in C++.
                    [color=blue]
                    > I've never come across operators before, what is their purpose in the
                    > great scheme of things?[/color]

                    You use operators all the time. +, -, <, >, ==, whatever are all operators.
                    C++ has the ability to create your own operators for your own types. What my
                    code did, was define a 'smaller than' operator for two objects of type
                    Allrecords. The code I put in the operator function is probably not what you
                    would want it to do though. You just need to make sure that the function
                    returns 'true' when 'left' is smaller than 'right' (ie. 'left' should appear
                    before 'right' in the sort order), and 'false' when 'left' and 'right' are
                    equivalent or 'right' is smaller.
                    [color=blue]
                    > std::vector<All records> is that to be declared before the operator
                    > function?[/color]

                    No, std::vector<All records> would be a replacement for Allrecords *rec.
                    vector is a (safer) alternative to an array, but as I said (and John
                    Harrison too) it's perfectly possible to sort the existing array using
                    std::sort.
                    [color=blue]
                    > This is the sort function I have, how can i change it so it works in
                    > the manner you suggested.
                    >
                    > the union object rec
                    > union Allrecords *rec
                    > is being sorted from a binary file.
                    > The idea behind this sort is to take two lines from the file and
                    > check that they null terminate '\0' because customer_code is 6 arrays
                    > long. Then use strcmp to compare the two, as Newcrecord.cust omer_code
                    > is declared as type char, use temp to store it while shuffling the
                    > pack around.
                    > As it is it doesn't work.[/color]

                    It's not exactly clear what you're trying to do. What sort algorithm are you
                    using? It looks a bit like Bubblesort, but it doesn't seem right. Are you
                    sure you need three nested loops? In any case, you shouldn't be reusing 'i'
                    in all three loops, because this way it'll have the value 'loop' when it
                    exits the innermost loop and therefore exits the outer loops as well. What
                    is 'loop' exactly? Is it the upperbound on rec (it looks like that anyway)?
                    What exactly is the definition of customercode? char or char* or char[]?
                    What is the criterea that defines whether to objects of type Allrecords are
                    equal, smaller or larger than each other? Could you perhaps give the entire
                    definition of Allrecords as well, including the structs in it, it would help
                    a lot.

                    --
                    Unforgiven

                    Comment

                    Working...