悉野小楼

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

c++ stl std::sort使用例子

例子1:
复制代码
class User
{
public:
    int32_t m_fight_power;
private:
    int32_t m_level;
};

bool CenterData::compare(const User *left, const User *right)
{
    if(left->m_fight_power != right->m_fight_power)
    {
        return left->m_fight_power > right->m_fight_power;
    }
    return false;
}

typedef std::vector<User*> UserVector;
UserVector userVector;
std::sort(userVector.begin(), userVector.end(), compare);
复制代码
例子2:
复制代码
struct DamageData
{
    EntityUUID player_id;
    int64_t damage;
    std::string player_name;
};
bool compareDamage(const std::shared_ptr<DamageData> left, const std::shared_ptr<DamageData> right)
{
    if (left->damage != right->damage)
    {
        return (left->damage > right->damage);
    }
    return false;
}
vector<std::shared_ptr<DamageData>> ranks;
std::sort(ranks.begin(), ranks.end(), compareDamage);
复制代码

 例子3:

复制代码
struct FieldDescriptor {
    int point;
};
//functor 仿函数
struct FieldNumberSorter {
    bool operator()(FieldDescriptor* left, FieldDescriptor* right) {
        if (left->point > right->point)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
};
int main()
{
    vector<const FieldDescriptor*> fields;
    std::sort(fields.begin(), fields.end(), FieldNumberSorter());
    system("pause");
    return 0;
}
复制代码

 

posted on   悉野  阅读(35)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
历史上的今天:
2014-08-25 C++引用与指针
点击右上角即可分享
微信分享提示