栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

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

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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


set容器重载 () 的方式

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

#include 
#include 
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& s) {
    for (set::const_iterator it = s.begin(); it != s.end(); it++) {
        cout << it->value << " ";
    }
    cout << endl;
}

int main(int argc, char const* argv[]) {
    set 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); 

    return 0;
}
set容器重载 < 方式

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

#include 
#include 
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& s) {
    for (set::const_iterator it = s.begin(); it != s.end(); it++) {
        cout << it->value << " ";
    }
    cout << endl;
}

int main(int argc, char const* argv[]) {
    set 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); 

    return 0;
}
map容器重载 () 的方式
#include 
#include 
#include 
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 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::const_iterator it = m.begin(); it != m.end(); it++) {
        cout << "name: " << it->first.name << " age: " << it->first.age << " value: " << it->second << endl;
    }
	
 
    return 0;
}
map容器重载 < 方式
#include 
#include 
#include 
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 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::const_iterator it = m.begin(); it != m.end(); it++) {
        cout << "name: " << it->first.name << " age: " << it->first.age << " value: " << it->second << endl;
    }

    

    return 0;
}
注意

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/676244.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号