简体   繁体   中英

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.

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.

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;
} );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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