1. 程式人生 > >stl set 使用結構體

stl set 使用結構體

#include <iostream>
#include <set>
#include <string>
using namespace std;

struct package
{
    int id;
    string data;
    bool operator<(const package& tmp) const{
        if(this->id < tmp.id)
            return true;   //自定義排序規則
        return false;
    }
};

int main() {
    set
<package>
tmp; tmp.insert({3,"a"}); tmp.insert({2,"b"}); //插入 set<package>::iterator i; for(i = tmp.begin(); i != tmp.end(); i++) cout << i->id << " " << i->data << endl; }