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

How does this code works ? ( multiset sorting )

struct compare{
    bool operator() ( const string& a , const string& b ) const{
        return a.size() < b.size ();
    }
};


multiset<string , compare> stg;

我正在解决一个问题,我想根据字符串长度对多重集进行排序。我在互联网上搜索并得到了这个结构。它对我有用..但我想知道它是如何工作的...

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

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

C++ 中的结构可以包含代码,但这通常不是一个好主意,因为您没有获得与 类 相同级别的封装。

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

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