C++中vector和set使用sort方法排序

C++中vector和set都是非常方便的容器,

sort方法是algorithm头文件里的一个标准函数,能进行高效的排序,默认是按元素从小到大排序

将sort方法用到vector和set中能实现多种符合自己需求的排序

①首先sort方法可以对静态的数组进行排序

#include<iostream>
using namespace std;
int main(){
    int a[10] = { 9, 0, 1, 2, 3, 7, 4, 5, 100, 10 };
    sort(a, a +10);
    for (int i = 0; i < 10; i++)
        cout << a[i] << endl;
    return 0;
}
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main(){
    vector<int> a;
    int n = 5;
    while (n--){
        int score;
        cin >> score;
        a.push_back(score);
    }
    //cout <<" a.end()"<< *a.end() << endl;       执行这句话会报错!
    cout << " prev(a.end)" << *prev(a.end()) << endl;
    sort(a.begin(), a.end());
    for (vector<int>::iterator it = a.begin(); it != a.end(); it++){
        cout << *it << endl;
    }
    return 0;
}

②用自定义的结构体进行sort算法

#include<iostream>
#include<vector>
#include<set>
#include<string>
#include<algorithm>
using namespace std;
struct student{ char name[10]; int score; }; //自定义"小于" bool comp(const student &a, const student &b){ return a.score < b.score; } int main(){ vector<student> vectorStudents; int n = 5; while (n--){ student oneStudent; string name; int score; cin >> name >> score; strcpy(oneStudent.name, name.c_str()); oneStudent.score = score; vectorStudents.push_back(oneStudent); } cout << "===========排序前================" << endl; for (vector<student>::iterator it = vectorStudents.begin(); it != vectorStudents.end(); it++){ cout << "name: " << it->name << " score: " << it->score << endl; } sort(vectorStudents.begin(),vectorStudents.end(),comp); cout << "===========排序后================" << endl; for (vector<student>::iterator it = vectorStudents.begin(); it != vectorStudents.end(); it++){ cout << "name: " << it->name << " score: " << it->score << endl; } return 0; }
#include<iostream>
#include<vector>
#include<set>
#include<string>
#include<algorithm>
using namespace std;
struct student{
    char name[10];
    int score;
    int age;
};
//自定义“小于”
bool comp(const student &a, const student &b){
    if (a.score > b.score)
        return true;
    else if (a.score == b.score  && a.age > b.age)
        return true;
    else                ///这里的else return false非常重要!!!!!
        return false;
}
int main(){
    vector<student> vectorStudents;
    /*set<student> setStudents;*/
    //int n = 5;
    int n = 6;
    while (n--){
        student oneStudent;
        string name;
        int score;
        int age;
        cin >> name >> score>>age;
        strcpy(oneStudent.name, name.c_str());
        oneStudent.score = score;
        oneStudent.age = age;
        vectorStudents.push_back(oneStudent);
    }
    cout << "===========排序前================" << endl;
    for (vector<student>::iterator it = vectorStudents.begin(); it != vectorStudents.end(); it++){
        cout << "name: " << it->name << " score: " << it->score << " age: "<<it->age<<endl;
    }
    sort(vectorStudents.begin(), vectorStudents.end(), comp);
    //sort(setStudents.begin(), setStudents.end());
    cout << "===========排序后================" << endl;
    for (vector<student>::iterator it = vectorStudents.begin(); it != vectorStudents.end(); it++){
        cout << "name: " << it->name << " score: " << it->score << " age: " << it->age << endl;
    }
    return 0;
}

③vector删除元素

vector<FaceInfo *>::iterator iter;
for (iter = faceinfoVec.begin(); iter != faceinfoVec.end();) {
     FaceInfo* itb = *iter;
     if (ita->m_captime > itb->m_captime + 5000) {
          faceinfoVec.erase(iter);
          delete itb;
          continue;
         }
  iter++;
}