"STL" overloaded operators

Source: Internet
Author: User

Overloaded operators

Why to overload operators:
The operand of a predefined operator in C + + can only be a basic data type. In practice, however, similar operations are required for many user-defined types, such as structs. These operators must be redefined in C + +, giving the existing operators new functionality that enables them to perform specific operations on specific types. The essence of operator overloading is function overloading, which provides the extensibility of C + + and is one of the most attractive features of C + +.
What is an overloaded operator:
Simply put: operator overloading, which is to redefine an existing operator and give it another function to accommodate different data types.
How to achieve:
Operator overloading is implemented by creating operator functions, which define the operations that the overloaded operators will take. The definition of an operator function is similar to that of other functions, except that the function name of the operator function is made up of the keyword operator and the operator symbol to be overloaded later.
The general format of operator overloading for member functions of a class is:
< function type > operator < operator > (< parameter table >) const
{
< function body >
}

Take the overloaded "<" operator as an example:

#include <iostream>#include<algorithm>using namespacestd;Const intmaxn= -;structNode//Define structure Body{    intA; intb; BOOL operator< (node tmp)Const//Overloaded operator Functions    {        if(a==tmp.a)returnb<tmp.b;//note the opening direction of the operator, which is sorted in ascending order of B (small to large)        returna>tmp.a;//sort in descending order of a (from large to small)}}E[MAXN];intN;intMain () {CIN>>N;  for(intI=1; i<=n;i++) Cin>>e[i].a>>e[i].b; Sort (e+1, e+n+1);//sort, because operators have been overloaded, so no more writing comparison functions     for(intI=1; i<=n;i++) cout<<e[i].a<<" "; return 0;}

Do not use overloaded operators:

#include <iostream>#include<algorithm>using namespacestd;Const intmaxn= -;structnode{intA; intb;} E[MAXN];intN;BOOLCMP (node X,node y)//sort function{    if(x.a==y.a)returnx.b<y.b; returnX.a>y.a;}intMain () {CIN>>N;  for(intI=1; i<=n;i++) Cin>>e[i].a>>e[i].b; Sort (e+1, e+n+1, CMP);//need to write a sort function cmp     for(intI=1; i<=n;i++) cout<<e[i].a<<" "; return 0;}

"STL" overloaded operators

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.