I have this code:
struct nod
{
nod *vCap;
int vCost;
char vInfo;
};
list<nod*> vList;
for (int i = 9; i >= 0; i--)
{
nod *vTmp;
vTmp->vCost=i;
vTmp->vInfo='a';
vList.push_back(vTmp);
}
How can I sort the list by the vCost value?
You’ll need a custom comparator to compare the field you’re interested in:
Then you can provide it as the comparator for
list::sort:In C++11, you can compress this into a lambda:
(Note that you almost certainly want to store objects, rather than pointers, in your list; in that case, change the comparator’s pointer arguments to references).