C + + programming excellent and Pascal is one of the reasons for the existence of STL (Standard Template Library) in C + +. There are many useful ways to STL.
Many methods in the C + + Template Library require an orderly correlation of parameters, such as sort (). Obviously, if you want to sort a set, you have to know the object in the collection, which is in the back of the previous one. Therefore, it is very important to learn how to define the comparison method.
Many containers for C + + template libraries require an orderly type of correlation, such as set<t> and priority_queue<t>.
This article is intended to tell you how to define a sort method for a class that can be used in an STL container or method. As a C + + programmer, you should know these methods.
How do I define a sort?
In short, to define a sort for a class, we can know that any two objects of the class are in the process of ordering who is in front of the other. We can do this by using a method that returns a bool value to indicate who is in the front row. Obviously, we want to implement a similar, F (x,y), this form of approach. It receives objects of the same type as two parameters, and the return value indicates who will appear in front of them.
Strict weak-order process
Almost all methods or containers need to be sorted to meet the strict and weak order of mathematical standards, otherwise the behavior of these methods or containers will be unpredictable.
Suppose F (x,y) is a comparison function. If the function satisfies the following conditions, it is strictly weakly ordered.
1.F (x,x) = false;
2. If f (x,y) then!f (y,x)
3.if f (X,y) and F (y,z) then F (x,z)
4. If!f (x,y) &&!f (y,x) then x==y; If X==y and y==z then x==z;
It looks a little dizzy, but don't worry, as long as your comparison method satisfies the return false of the equality element, your method will be satisfied.
Three ways to implement:
1. Definition < operator.
Using this method enables our custom classes to have the innate ability to sort. For example, if you have the following classes:
struct Edge
{
int from,to, weight;
};
Because to implement the Kruskai algorithm, you want all edges in the graph to be sorted in descending order according to weight. Define OPERATOR< like this:
struct Edge
{
int from,to, weight;
BOOL Operator < (Edge other) const
{return
weight>other.weight;
}
};
The method you define must be declared in the following way:
BOOL operator< (T Other) const
Note: The const keyword is required.
If you don't like this way, for example, if you want to compare two objects, the method has only one argument. You can choose the following ways:
struct Edge
{
int from,to weight;
friend bool operator< (Edge A,edge b)
{return
a.weight>b.weight;
}
};
The pair<t1,t2> of STL has the innate ability of sorting. Two comparison of the pair objects: first, compare the first argument, and then compare the second argument if the first argument is the same.
All built-in types have the innate ability to sort, which is given by the compiler.
2. Custom sorting methods.
This approach is often used in the following situations:
A. Comparing built-in types
B. Types that need to be compared cannot be modified
C. Comparison methods other than type-customized comparisons
In simple terms, a comparison method receives two objects of the same type as a parameter and returns a bool value, which is the following prototype:
BOOL Name (T a,t b);
3. Overload () operator
We can use the comparison function as the first parameter of the STL container constructor, and the function type as the template parameter. For example:
Set<int,bool (*) (Int,int) > S (CMP);
This is more or less confusing. Let's take a look at how to use the imitation function to dispel your doubts.
We need to define a new class and overload () the operator.
vector<int> occurrences;
struct CMP
{
bool operator (int a, int b) {return
Occurrences[a] < occurrences[b];
}
;
Now we can pass this class as a template parameter to the STL container.
Set<int, cmp> s;
Priority_queue<int, Vector<int>, Cmp> PQ;
STL also has some built-in imitation functions, such as less<t>,greater<t> and so on.
An imitation function can be initialized and then used like a normal function. The simplest is to add () to the back of the imitation function.
Sort (Data.begin (), Data.end (), greater<int> ());
The above is a small series for everyone on the definition of C + + function in three ways summary of all the content, I hope that we support cloud-Habitat Community ~