<分区>
Possible Duplicate:
How to use std::sort with a vector of structures and compare function?
我有一个猫对象(什么?)和一个显然对猫对象进行排序的 catSort 对象。下面是类
class cat {
public:
int age;
};
class catSorter {
public:
vector< cat > cats;
vector< cat > SortCatsByAge();
void AddCat( cat new_cat );
};
void catSorter::AddCat(cat new_cat){
this->cats.push_back(new_cat)
}
vector< cat > catSorter::SortCatsByAge(){
// Sort cats here by age!
}
cat tim;
tim.age = 10;
cat mark;
mark.age = 20
cat phil;
phil.age = 3;
catSorter sorter;
sorter->AddCat(tim);
sorter->AddCat(mark);
sorter->AddCat(phil);
std::<vector> sortedcats = sorter->SortCatsByAge();
我在对 vector 进行排序时遇到困难,我该怎么做呢?我是否应该循环遍历 cats
属性并将它们存储在一个临时 vector 中然后返回它?有更简单的方法吗?