众所周知,使用sort算法+自定义compare函数对自定义结构体进行排序:
struct NumCount
{
int num, count;
NumCount(int n,int c):num(n),count(c){
}
};
bool compare2(NumCount* a, NumCount* b) {
return a->num < b->num;
}
void printNcs2(const vector<NumCount*>& ncs) {
for (auto nc : ncs) {
cout << nc->num << ' ' << nc->count << endl;
}
}
void addNc(vector<NumCount*>& ncs, int n, int c) {
NumCount* nc = new NumCount(n, c);
ncs.push_back(nc);
<