List sorting and comparison functions are passed as parameters

Source: Internet
Author: User

A problem occurred when using the list sorting function a few days ago.

Defines a simple node class, including a string and an integer, and reloads the less than operation to compare it based on the integer size.

class Node{    public:        Node(string nm,int i)        {            name = nm;            id = i;        }        void Print()        {            cout << name << ":" << id << endl;        }        friend bool operator < (const Node& n1,const Node& n2)        {            cout << "comparing:" << endl;            return (n1.id<n2.id);        }    public:        int id;        string name;};

Then a test structure is created,
Put the addresses of some Node nodes into the list, which is usually used in this way, because these nodes are sometimes created in one place and used in other places,
If new is not used during creation, after they are saved to the list, they cannot be used in other places, so they can only be created with new and then put the pointer into the list.
The Code is as follows,

class NodeList{    public:        NodeList()        {            Node* n1 = new Node("abc",3);            Node* n2 = new Node("efb",5);            Node* n3 = new Node("ac",1);            Node* n4 = new Node("a",0);            Node* n5 = new Node("4",4);            nodes.push_back(n1);            nodes.push_back(n2);            nodes.push_back(n3);            nodes.push_back(n4);            nodes.push_back(n5);        }        void Print()        {            list<Node*>::iterator it = nodes.begin();            for(;it!=nodes.end();it++)            {                (*it)->Print();            }        }        void Test()        {            Print();            nodes.sort();            cout << "\nafter sort:" << endl;            Print();            struct cmp_node cmpnode;            nodes.sort(cmpnode);            cout << "\nafter sort(cmp_node):" << endl;            Print();        }    public:        list<Node*> nodes;};

When using list sort, you can use the default parameter or pass a comparison function. Therefore, a Node comparison function is defined and placed in struct,

struct cmp_node{    public:        bool operator () (const Node* n1, const Node* n2) const        {            return ((n1->id)<(n2->id));        }};

Finally, the Test function of nodeList can be called. What are the expected results of the next two queries in order? However, only the last result is sorted, that is, for the first time, the sort that does not pass the comparison function as the parameter does not sort the list. I don't know why.
Let's take a look at why we should put the newly defined comparison function in a struct.
In C/C ++, a function is not an object and cannot be passed as a parameter. Therefore, put the function to be passed in an object, which is placed in a struct, it can also be put in a class as a public function. It has not been tested. Remember that it is similar when the unordered_map function is used to pass the hash function. The last method was used at that time.
Note that when defining a comparison function in a struct, we do not need to reload the less than sign, but a pair of parentheses. Then, the parameter should be the data type in the list, which is a node pointer.

It seems awkward to put the pointer in the list. I hope you can guide the usage of the list in this case.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.