Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Sorting a list with class for complex types [modified]

Sorting a list with class for complex types [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
algorithms
31 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Harold_Wishes

    This worked. But I still have commented out the code that is suppose to do the sort---> mylist.sort(MyDataSortPredicate);

    C:\Documents and Settings\WoodallH\Desktop\C++\Project 6 Nesty tag\Sort.cpp(38) : error C2664: 'void __thiscall std::list<class MyData,class std::allocator<class MyData> >::sort(struct std::greater<class MyData>)' : cannot convert parameter 1 from '
    bool (const class MyData &,const class MyData &)' to 'struct std::greater<class MyData>'
    No constructor could take the source type, or constructor overload resolution was ambiguous
    Error executing cl.exe.

    -- modified at 15:03 Monday 10th July, 2006

    Z Offline
    Z Offline
    Zac Howland
    wrote on last edited by
    #21

    You can either use the general sort algorithm (std::sort) or write a specialized less<MyData>() functor that looks something like:

    struct std::less
    {
    	bool operator()(const MyData& lhs, const MyData& rhs)
    	{
    		return lhs.m_iData < rhs.m_iData;
    	}
    };
    

    And then call mylist.sort(std::less<MyData>). If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

    1 Reply Last reply
    0
    • H Harold_Wishes

      Hello I have designed a program that takes in a list of structs of type data (shown below). The program works fine. But I need a way of sorting the list by decreasing length of string Sequence. In other words, I need to determine the length of each Sequence and position each data so that the longest strings appear first. So I am not comparing strings themselves, but lengths of strings. I know there is a sort function that is part of the <list> class, but I am not sure if I can use it in this situation. Thanks in advance for anyone who can come to a solution. HRW.

      #include <string>
      #include <list>
      #include <iostream>
      #include <fstream>

      using namespace std;

      struct data //
      {
      string Length; //
      string Sequence; //
      string N_Terminal; //
      string C_Terminal;
      };

      list<data> g_DataList;
      list<data>::iterator dataListIter;

      -- modified at 11:29 Monday 10th July, 2006

      E Offline
      E Offline
      earl
      wrote on last edited by
      #22

      Harold, You'll do much better if you get (1) a copy of Stroustrup and (2) a copy of Josuttis. Read them in that order. Because what you want to do is trivial and will be explained by the first book; putting in the time for the second will be worth it. earl

      H 1 Reply Last reply
      0
      • E earl

        Harold, You'll do much better if you get (1) a copy of Stroustrup and (2) a copy of Josuttis. Read them in that order. Because what you want to do is trivial and will be explained by the first book; putting in the time for the second will be worth it. earl

        H Offline
        H Offline
        Harold_Wishes
        wrote on last edited by
        #23

        Are these C++ authors of C++ books? I will look for them. I did not see anything in the Deitel & Deitel book that was too helpful.

        E 1 Reply Last reply
        0
        • H Harold_Wishes

          Are these C++ authors of C++ books? I will look for them. I did not see anything in the Deitel & Deitel book that was too helpful.

          E Offline
          E Offline
          earl
          wrote on last edited by
          #24

          Hit up amazon for those two. Stroustrup largely invented C++ and wrote one of the definitive books on it; Josuttis wrote a book on the standard library that is a great complement to the former.

          H 1 Reply Last reply
          0
          • H Harold_Wishes

            Hello I have designed a program that takes in a list of structs of type data (shown below). The program works fine. But I need a way of sorting the list by decreasing length of string Sequence. In other words, I need to determine the length of each Sequence and position each data so that the longest strings appear first. So I am not comparing strings themselves, but lengths of strings. I know there is a sort function that is part of the <list> class, but I am not sure if I can use it in this situation. Thanks in advance for anyone who can come to a solution. HRW.

            #include <string>
            #include <list>
            #include <iostream>
            #include <fstream>

            using namespace std;

            struct data //
            {
            string Length; //
            string Sequence; //
            string N_Terminal; //
            string C_Terminal;
            };

            list<data> g_DataList;
            list<data>::iterator dataListIter;

            -- modified at 11:29 Monday 10th July, 2006

            S Offline
            S Offline
            Stephen Hewitt
            wrote on last edited by
            #25

            Simply define an ordering by implementing operator <. i.e. struct data {     string Length;     string Sequence;     string N_Terminal;     string C_Terminal;       friend bool operator<(const data &L, const data &R)     {        // In this example I'm only sorting by 'Sequence'        return L.Sequence<R.Sequence;     } };   // Now you can sort like this. g_DataList.sort(); Steve

            H 2 Replies Last reply
            0
            • S Stephen Hewitt

              Simply define an ordering by implementing operator <. i.e. struct data {     string Length;     string Sequence;     string N_Terminal;     string C_Terminal;       friend bool operator<(const data &L, const data &R)     {        // In this example I'm only sorting by 'Sequence'        return L.Sequence<R.Sequence;     } };   // Now you can sort like this. g_DataList.sort(); Steve

              H Offline
              H Offline
              Harold_Wishes
              wrote on last edited by
              #26

              So after creating the list, doesn't the sort function take in one parameter of type data? And will the list be printed in sorted order after the sort function is invoked?

              S 1 Reply Last reply
              0
              • H Harold_Wishes

                So after creating the list, doesn't the sort function take in one parameter of type data? And will the list be printed in sorted order after the sort function is invoked?

                S Offline
                S Offline
                Stephen Hewitt
                wrote on last edited by
                #27

                The list already knows the type of the data it contains. There are two member functions of list called sort: one with no parameters and one which takes a predicate. I'm using the one which takes no parameters and overloading operator <. Give it a try and see if it works for you. Steve

                1 Reply Last reply
                0
                • E earl

                  Hit up amazon for those two. Stroustrup largely invented C++ and wrote one of the definitive books on it; Josuttis wrote a book on the standard library that is a great complement to the former.

                  H Offline
                  H Offline
                  Harold_Wishes
                  wrote on last edited by
                  #28

                  I did check amazon and saw several books by Stroustrup. Do you know the title of the book? -- modified at 21:13 Monday 10th July, 2006

                  E 1 Reply Last reply
                  0
                  • S Stephen Hewitt

                    Simply define an ordering by implementing operator <. i.e. struct data {     string Length;     string Sequence;     string N_Terminal;     string C_Terminal;       friend bool operator<(const data &L, const data &R)     {        // In this example I'm only sorting by 'Sequence'        return L.Sequence<R.Sequence;     } };   // Now you can sort like this. g_DataList.sort(); Steve

                    H Offline
                    H Offline
                    Harold_Wishes
                    wrote on last edited by
                    #29

                    Thanks! This really helps because my goal is to actually compare the string length of the Sequence variable and assign that value to int Number as I have it defined below:

                    struct data //
                    {
                    int Number;
                    string Length;
                    string Sequence;
                    string N_Terminal;
                    string C_Terminal;
                    friend bool operator<(const data &L, const data &R)
                    {
                    return L.number > R.number;
                    }

                    };

                    Then I invoke the sort after the list has been populated with items.

                    g_DataList.sort();

                    You will notice I overloaded the > operator instead of the < operator since I need the longest strings to appear first. The thing I do not quite get is how the compiler knows what const data &L and const data &R are when they are passed by reference in the friend function. I'm not making the connection between the comparison and the sort function? Perhaps I do not understand the sorting algorithym. I confess my lack of programming experience even though I have the program working like a champ at this stage. Regards, HRW. :) -- modified at 4:40 Tuesday 11th July, 2006

                    S 1 Reply Last reply
                    0
                    • H Harold_Wishes

                      Thanks! This really helps because my goal is to actually compare the string length of the Sequence variable and assign that value to int Number as I have it defined below:

                      struct data //
                      {
                      int Number;
                      string Length;
                      string Sequence;
                      string N_Terminal;
                      string C_Terminal;
                      friend bool operator<(const data &L, const data &R)
                      {
                      return L.number > R.number;
                      }

                      };

                      Then I invoke the sort after the list has been populated with items.

                      g_DataList.sort();

                      You will notice I overloaded the > operator instead of the < operator since I need the longest strings to appear first. The thing I do not quite get is how the compiler knows what const data &L and const data &R are when they are passed by reference in the friend function. I'm not making the connection between the comparison and the sort function? Perhaps I do not understand the sorting algorithym. I confess my lack of programming experience even though I have the program working like a champ at this stage. Regards, HRW. :) -- modified at 4:40 Tuesday 11th July, 2006

                      S Offline
                      S Offline
                      Stephen Hewitt
                      wrote on last edited by
                      #30

                      A friend function is really a global function, not a member function. This would be clearer if the declaration and the definition were separated as is shown below: struct data {     int Number;     string Length;     string Sequence;     string N_Terminal;     string C_Terminal;     friend bool operator<(const data &L, const data &R); // Declaration. };   // Definition. bool operator<(const data &L, const data &R) {     return L.number > R.number; } In this example the function doesn't even need to be made a friend as it doesn't access any protected or private members. With this modification it looks like this: struct data {     int Number;     string Length;     string Sequence;     string N_Terminal;     string C_Terminal; };   bool operator<(const data &L, const data &R) {     return L.number > R.number; } When the compiler sees a < and one or both of the parameters (the expressions to the left and right of the <) is a user defined type it looks for a user defined operator<. It chooses from all the candidates by matching the types in the expression it is compiling with the types of the operator. Steve

                      1 Reply Last reply
                      0
                      • H Harold_Wishes

                        I did check amazon and saw several books by Stroustrup. Do you know the title of the book? -- modified at 21:13 Monday 10th July, 2006

                        E Offline
                        E Offline
                        earl
                        wrote on last edited by
                        #31

                        http://www.amazon.com/gp/product/0201700735/sr=8-1/qid=1152627803/ref=pd\_bbs\_1/002-6564248-4536802?ie=UTF8 http://www.amazon.com/gp/product/0201379260/002-6564248-4536802?n=283155

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups