STL之set/multiset(二)

来源:互联网 发布:lambda表达式java 编辑:程序博客网 时间:2024/06/06 00:52
---------------------------------------------------- find(key)查找函数之自定义的数据类型 ----------------------------------------------------#include <iostream>#include <set>#include <string>#include <stdlib.h>using namespace std;class Student{public:    Student(string name,int age)    {        this->m_name = name;        this->m_age = age;    }    Student(){}    ~Student(){}public:    bool operator<(const Student& s) const    {        return this->m_age < s.m_age;    }public:    string m_name;    int m_age;};void test00(){    set<Student> s;    s.insert(Student("aaa",22));    s.insert(Student("bbb",23));    s.insert(Student("ccc",24));    s.insert(Student("ddd",25));    set<Student>::iterator it = s.find(Student("ccc",24));    if (it == s.end())    {        cout<<"查找失败!"<<endl;    }    else    {        cout<<"查找成功!"<<endl;        cout<<it->m_name<<" "<<it->m_age<<endl;    }}int main(){    test00();    system("pause");    return 0;}结果:查找成功!ccc  24
原创粉丝点击