简体   繁体   English

对结构的结构列表进行排序

[英]Sorting a struct list of struct

I have two structs:我有两个结构:

struct memBlock {
    uint32_t  pid;
    uint32_t  moduleSize;
    void*  moduleStart;
};

struct memory{
    uint32_t  memSize;
    uint32_t  memSizeOS;
    uint32_t  chunkSize;
    uint32_t  nextFit;
    list<memBlock> freeMem;
    list<memBlock> usedMem;
};

memory memo;

I want to sort the freeMem list of memo in ascending order of moduleSize.我想按 moduleSize 的升序对备忘录的 freeMem 列表进行排序。

How can I accomplish this?我怎样才能做到这一点?

I tried this:我试过这个:

sort( memo.freeMem.begin( ), memo.freeMem.end( ), 
                []( const memory& a, const memory&b ){
                return a.freeMem < b.freeMem;
            } );

but I don't know how to specify that sort should consider moduleSize variable.但我不知道如何指定排序应该考虑 moduleSize 变量。

Solution:解决方案:

memo.freeMem.sort([]( memBlock a,memBlock b){return a.moduleSize<b.moduleSize;});

Probably you want something like this:可能你想要这样的东西:

sort( memo.freeMem.begin( ), memo.freeMem.end( ), 
   []( const memblock& a, const memblock&b ){
   return a.moduleSize < b.moduleSize;
} );

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

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