C++ STL set和map容器插入自定义数据

自定义的数据类型需要指定排序的方式,才可以使用set或者map容器。


set容器重载 () 的方式

需要额外定义一个类,类中重载()来指定排序的方式,Compare中指定了排序为从大到小的排序。

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

class MyData {
   public:
    int value;
    MyData(int value) {
        this->value = value;
    }
};

class Compare {
   public:
   	// 注意:参数一定要加const
    bool operator()(const MyData& p1, const MyData& p2) {
        return (p1.value > p2.value);
    }
};

void printSet(set<MyData, Compare>& s) {
    for (set<MyData>::const_iterator it = s.begin(); it != s.end(); it++) {
        cout << it->value << " ";
    }
    cout << endl;
}

int main(int argc, char const* argv[]) {
    set<MyData, Compare> s;

    MyData p1(1);
    MyData p2(3);
    MyData p3(4);
    MyData p4(2);

    s.insert(p1);
    s.insert(p2);
    s.insert(p3);
    s.insert(p4);

    printSet(s); /* 输出 4 3 2 1 */

    return 0;
}

set容器重载 < 方式

如果不想额外定义类来实现指定排序,可以在原始类中直接重载<实现指定排序。

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

class MyData {
   public:
    int value;
    MyData(int value) {
        this->value = value;
    }

	// 注意:一定要加const
    bool operator<(const MyData& p) const {
        return (this->value > p.value);
    }
};

void printSet(set<MyData>& s) {
    for (set<MyData>::const_iterator it = s.begin(); it != s.end(); it++) {
        cout << it->value << " ";
    }
    cout << endl;
}

int main(int argc, char const* argv[]) {
    set<MyData> s;

    MyData p1(1);
    MyData p2(3);
    MyData p3(4);
    MyData p4(2);

    s.insert(p1);
    s.insert(p2);
    s.insert(p3);
    s.insert(p4);

    printSet(s); /* 输出 4 3 2 1 */

    return 0;
}

map容器重载 () 的方式

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

class Test {
   public:
    string name;
    int age;
    Test(string name, int age) {
        this->name = name;
        this->age = age;
    }
};

class Compare {
   public:
    bool operator()(const Test& t1, const Test& t2) {
        if (t1.age == t2.age) {
            return (t1.name > t2.name);
        } else {
            return (t1.age > t2.age);
        }
    }
};

int main(int argc, char const* argv[]) {
    Test t1("xiaohu", 18);
    Test t2("pdd", 21);
    Test t3("xiaoxiao", 23);
    Test t4("kid", 20);
    Test t5("khan", 20);

    map<Test, int, Compare> m;
    m.insert(make_pair(t1, 1));
    m.insert(make_pair(t2, 2));
    m.insert(make_pair(t3, 3));
    m.insert(make_pair(t4, 4));
    m.insert(make_pair(t5, 5));

	for (map<Test, int, Compare>::const_iterator it = m.begin(); it != m.end(); it++) {
        cout << "name: " << it->first.name << " age: " << it->first.age << " value: " << it->second << endl;
    }
	/* 输出:
        name: xiaoxiao age: 23 value: 3
        name: pdd age: 21 value: 2
        name: kid age: 20 value: 4
        name: khan age: 20 value: 5
        name: xiaohu age: 18 value: 1
     */
 
    return 0;
}

map容器重载 < 方式

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

class Test {
   public:
    Test(string name, int age) {
        this->name = name;
        this->age = age;
    }

    bool operator<(const Test& t) const {
        if (this->age == t.age) {
            return (this->name > t.name);
        } else {
            return (this->age > t.age);
        }
    }

    string name;
    int age;
};

int main(int argc, char const* argv[]) {
    Test t1("xiaohu", 18);
    Test t2("pdd", 21);
    Test t3("xiaoxiao", 23);
    Test t4("kid", 20);
    Test t5("khan", 20);

    map<Test, int> m;
    m.insert(make_pair(t1, 1));
    m.insert(make_pair(t2, 2));
    m.insert(make_pair(t3, 3));
    m.insert(make_pair(t4, 4));
    m.insert(make_pair(t5, 5));

    for (map<Test, int>::const_iterator it = m.begin(); it != m.end(); it++) {
        cout << "name: " << it->first.name << " age: " << it->first.age << " value: " << it->second << endl;
    }

    /* 输出:
        name: xiaoxiao age: 23 value: 3
        name: pdd age: 21 value: 2
        name: kid age: 20 value: 4
        name: khan age: 20 value: 5
        name: xiaohu age: 18 value: 1
     */

    return 0;
}

注意

无论是重载的()还是重载的<,都一定要加const,否则会报错。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值