这些代码是如何工作的?(多集排序)

How does this code works ? ( multiset sorting )

本文关键字:排序 工作 代码 何工作      更新时间:2023-10-16
struct compare{
    bool operator() ( const string& a , const string& b ) const{
        return a.size() < b.size ();
    }
};

multiset<string , compare> stg;

我正在解决一个问题,我想根据字符串长度对multiset进行排序。我在网上搜索了一下,得到了这个结构。这对我很有效……但我想知道这是如何工作的…

我只是对这行代码感到困惑。直到这一点,我认为结构只能包含变量,而不是函数…是不是有点像类中的重载。

 bool operator() ( const string& a , const string& b ) const{

c++中的结构可以包含代码,但这通常是一个坏主意,因为您无法获得与类相同级别的封装。

但是,C语言中的结构体只是结构体,不能包含代码。

来源:https://msdn.microsoft.com/en-us/library/4a1hcx0y.aspx