The study of C + + operator

Source: Internet
Author: User
<span style= "font-family:arial, Helvetica, Sans-serif;" >priority_queue is relatively simple to use for basic types. His template declaration has three parameters, type is the data type, Container is the container that holds the data, functional is the element comparison method. Container must be an array-implemented container, such as vector, deque but not a list. STL inside the container by default is vector. The comparison method defaults to operator<, so if you take the next two parameters by default, the priority queue is the top heap, and the team head element is the largest. </span>
Instance
#include <queue>
using namespace std;
int main () {
    priority_queue<int,vector<int>,less<int> >q1;//uses priority_queue<int> Q1; The same, is the big pile, yes, is Dagen for
    (int i=0;i<10;i++) 
		q1.push (i);
    while (!q1.empty ()) {
        cout<<q1.top () << Endl;
        Q1.pop ();
    }
    return 0;
}
If you want to use a small top heap, you typically take the three parameters of the template.
An affine function greater<&gt is defined in the STL, and for the base type, the small top heap can be declared with this affine function.
Example: Change less<int> for greater<int>

#include <iostream>
#include <queue>
using namespace std;
int main () {
    priority_queue<int,vector<int>,greater<int> >q;
    for (int i=0;i<10;i++) 
		q.push (i);
    while (!q.empty ()) {
        cout<<q.top () << Endl;
        Q.pop ();
    }
    return 0;
}

Custom type, to overload the operator, because the priority_queue default is Operator<,so overload this (remember.) )

Two of them are equivalent.

1:

#include <iostream>
#include <queue>
using namespace std;
struct node{
	int x, y;
	BOOL operator< (const Node &b) const//Why so many const (read-only) and & (Reference) <span style= "Font-family:arial, Helvetica, Sans-serif; " >   The issue is described below, which will refer to </span>
	{
    	if (x==b.x) return y>b.y;
    	Return x>b.x;
	} is defined as a member function, which functions only when the left side of the operator is the struct variable
}node;
 
 int main () {
    priority_queue<node>q;
    for (int i=0;i<10;i++) {
    	node.x=i;
    	NODE.Y=10-I/2;
    	Q.push (node);
    }	
    while (!q.empty ()) {
        cout<<q.top (). x << ' <<q.top () .y<<endl;
        Q.pop ();
    }
    return 0;
}

2:

#include <iostream>
#include <queue>
using namespace std;
struct node{
	int x, y;
} node;
 BOOL operator< (Node A, Node B) {
    if (a.x==b.x) return a.y>b.y;
    Return a.x>b.x;
} Defined outside the structure, as a member of the global namespace, defined outside, the left side of the operator may not be a struct variable, that is, the node in the previous one here is allowed to be int
Also, outside the structure, you don't have to put the troublesome const &
 int main () {
    priority_queue<node>q;
    for (int i=0;i<10;i++) {
    	node.x=i;
    	NODE.Y=10-I/2;
    	Q.push (node);
    }	
    while (!q.empty ()) {
        cout<<q.top (). x << ' <<q.top () .y<<endl;
        Q.pop ();
    }
    return 0;
}

C: How to decide to overload an operator with a class member function or a member of the global namespace?
① If an overloaded operator is a class member, the operator is invoked only if the left operand with which it is used is an object of that class. If the left-hand operand of the operator must be of another type, the operator must be overloaded as a member of the global namespace.
②c++ requires assignment =, subscript [], call (), and the member pointing to the-> operator must be defined as a class member operator. Any definition that defines these operators as namespace members is marked as a compile-time error.
③ if one operand is a case of a class type such as a string class, it is best defined as a global namespace member for a symmetric operator such as an equal operator.



Attention:

There has always been a problem that you don't understand, for sort, the CMP function is as long as it is, then it is less than, then ascending;> is from big to small

But looking at many other definitions, this is not the case, contrary to the imagination.

struct node{
<span style= "White-space:pre" >	</span>int x, y;
<span style= "White-space:pre" >	</span>bool operator< (const Node &b) const
<span style= "White-space:pre" >	</span>{
<span style= "White-space:pre" >		</span>if (x==b.x) return y>b.y;
<span style= "White-space:pre" >		</span>return x>b.x;
<span style= "White-space:pre" >	</span>}//For example here, this is the above precedence queue overload, meaning that by x as the first keyword, y is the second keyword, the small Gan, But notice the operator <
The following became x>b.x, wrong AH. In fact, the priority queue is a heap inside the operator, which is the default operator, but we give him the meaning of >, why to Give > meaning n. Because, inside the Priority_pueue, the default is the large root heap, that is, if the root node <span style= "color: #ff0000;" > Less than </span> a child, then the Exchange (< is used here) we have to change him to a small Gan, then it is if the root node is large, the exchange of,< symbol has not changed, but only to represent >. X is the operator to the left, b.x nature is the right.
}node;
It's almost clear.

But what about the cosnt and &?

Understand some concepts

1. For example, you define a function void Add (int a, int b), where A and b are formal parameters.
2. When you make a function call, add (1, 2), where 1 and 2 are the arguments.

BOOL operator< (const Node &b) Const
{
    	if (x==b.x) return y>b.y;
    	Return x>b.x;
}

Just the question, if the first const and & deleted, nothing;& deleted nothing; the second const deleted;

And if the first const is deleted (& does not move), it will also make an error.

Because: if the only purpose of using a reference type's arguments is to avoid a large number of duplicate arguments and not to make modifications to the arguments, it is best to use the parameter type of const type name &. This is why the above with the const node &b, so be sure to add, the second const directly on the back.

See: http://blog.csdn.net/guiyinzhou/article/details/6298030 C + + "Const reference parameter" "Class member function"



The code writes very excrement, mainly is to practice the class the overloaded operator, understood the const exactly is uses: [CPP] view plain copy print?   BOOL operator < (const point B) const{return a.x<b.x; }

The first const guarantees that B is not changed, and the second const guarantees that the original variable is not changed.

Write these roughly to make sure that the unchanged is not changed (otherwise there will be a compilation error) ....


Related Article

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.