#include <iostream>
#include <set>
#include <string>
using namespace std;
struct Word
{
string str;
int prob;
Word( string s, int i):str(s),prob(i){};
Word(){};
};
struct MapCmp
{
bool operator()(const Word s1, const Word s2) const
{
if( s1.prob != s2.prob )
return s1.prob > s2.prob;
return s1.str.compare(s2.str) > 0;
}
};
typedef multiset<Word, MapCmp> Mapwords;
Mapwords words;
int main()
{
words.insert(Word("hello",1));
words.insert(Word("hell",1));
words.insert(Word("hell",2));
words.insert(Word("third",3));
words.insert(Word("ok",3));
words.insert(Word("jjjj",-2));
Mapwords::iterator mbegin;
Mapwords::iterator mend = words.end();
for( mbegin = words.begin(); mbegin != mend; ++mbegin)
cout<<mbegin->str<<'\t'<<mbegin->prob<<endl;
getchar();
return 0;
}
C++ STL中的set如何对自定义类对象排序?
最新推荐文章于 2024-01-06 13:42:52 发布