C++ about sort of sorting structs in STL

  • 2020-04-01 21:40:51
  • OfStack

preface

I have not read c++ system, because I know some basic syntax of c, I use c++ in actual programming, I can only use what to see what, I found that although this can complete most of the work, but sometimes the efficiency is too low, such as this section to talk about the use of Std::sort() function, it took half a day to adjust. Open c/c++ sequence blog is to record some problems in the use of c++, to avoid future mistakes, of course, later will try to make time to learn c++ system.

Development environment: QtCreator2.5.1+OpenCV2.4.3

Experimental basis

First, let's take a look at the use of the quick sort algorithm sort in STD:

The template < The class RandomAccessIterator, class Compare>   Void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp);

This is a function with template. Parameters 1 and 2 indicate the starting and ending positions of the elements to be sorted in the random iterator. The data type pointed by the iterator can be defined by itself. The parameter comp is used to determine whether the sort is in ascending or reverse order. By default, it is in ascending order. But the advantage of this default is that the iterator points to a common data type, such as integer, character, and so on. If the data type is a class or struct and an element in that class or struct is used for sorting, then you need to define your own sort overloading symbol "<". . For example, in this experiment, the overloaded symbol is defined as:



bool compare(const PAIR &x, const PAIR &y)
{
    return x.point_value > y.point_value;
}

If you define the comp as a function (which seems to be used a lot on the Internet), for example, the function is as follows:


bool operator<(const PAIR &x, const PAIR &y)
{
    return x.point_value > y.point_value;
}

The following error will be reported:

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201304/2013042511565348.png ">

STD ::sort cannot derive template parameters, etc., because the function parameters are not specified.

 

The experimental results

The experiment is based on a problem: there are some sets of coordinate points (2d coordinate points, no repetition between coordinate points), each coordinate point corresponds to a number, and now we need to sort these Numbers to achieve the order of these coordinate points. There was an attempt to put the coordinates of the points and their corresponding values in a map, and then sort the elements in the map with STD ::sort(), but the debugging failed because the overloading symbol was not found at first. Now instead of a map, we're going to use a vector, and a vector is going to contain structs with coordinate points and their corresponding values.

In this experiment, three structural objects are stored in the vector, and a two-dimensional point and its corresponding value are put into each structure, and then sort by sort(). The sorting results are as follows:

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201304/2013042511565349.png ">

Experimental code and comments

The main. CPP:


#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace std;
using namespace cv;
typedef struct
{
    cv::Point point;
    long point_value;
} PAIR;

bool operator<(const PAIR &x, const PAIR &y)
{
    return x.point_value > y.point_value;
}
//
//bool compare(const PAIR &x, const PAIR &y)
//{
//    return x.point_value > y.point_value;
//}
void main()
{
    PAIR pair1, pair2, pair3;
    std::vector<PAIR> vec;
    pair1.point = Point(10, 20);
    pair1.point_value = 100;
    pair2.point = Point(70, 30);
    pair2.point_value = 99;
    pair3.point = Point(44, 76);
    pair3.point_value = 101;
    vec.push_back(pair1);
    vec.push_back(pair2);
    vec.push_back(pair3);
//    std::sort(vec.begin(), vec.end(), compare);
    std::sort(vec.begin(), vec.end());
    cout << " The result of sorting is zero :" << endl;
    for(vector<PAIR>::iterator it = vec.begin(); it != vec.end(); ++it) {
        cout << it->point << endl;
    }
    return ;
}

The experimental conclusion

The STD ::sort() function is powerful and can sort classes, structs, etc.


Related articles: