简体   繁体   English

C++ 帮助 STL - sort() function

[英]C++ help with STL - sort() function

Another small question about STL:另一个关于STL的小问题:

i have Dictionary:我有字典:

map <string,vector <Wordy> > Dictionary;

using structure Wordy:使用结构 Wordy:

struct Wordy{ int count; string word;}

also this structure have overloaded operator<这个结构也有重载 operator<

bool operator< (Wordy& One, Wordy& Two){return One.count<Two.count;}

but this sort() function from algorithm doesn't work!但是算法中的这种 sort() function 不起作用!

sort(Dictionary.find(s)->second.begin(),Dictionary.find(s)->second.end());

Your operator< should take its parameters by reference-to-const, I think that might be it:您的operator<应该通过引用到常量来获取其参数,我认为可能是这样:

bool operator< (const Wordy& One, const Wordy& Two){return One.count<Two.count;}
//              ^^^^^             ^^^^^

check the post How to use std::sort with a vector of structures and compare function?检查帖子如何将 std::sort 与结构向量一起使用并比较 function? . . it explains how to use sort with a customized predicate function它解释了如何使用自定义谓词 function 进行排序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM