stl map 使用结构体作为键

#include 
#include 
#include 
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() {
    mapint> tmp;
    package a = {3,"a"};
    package b = {2,"b"};
    tmp.insert(make_pair(a, 0));
    tmp.insert(make_pair(b, 0));  //插入
    mapint>::iterator i;
    for(i = tmp.begin(); i != tmp.end(); i++)
        cout << i->first.id << " " << i->first.data << " " << i->second << endl;
}

你可能感兴趣的:(c++)