WeiHandsome 2023-07-10 21:52 采纳率: 85%
浏览 41
已结题

c++ binary_search 搜索自定义数据类型

求大家帮我看看binary_search如何完成自定义数据类型搜索,主要是第二个案例



#include <iostream>
using namespace std;
#include <algorithm>
#include<vector>
#include<string>
#include<ctime>
class Person
{
public:
    Person(string name, int age);
    ~Person();

    bool operator==(const Person& p)
    {
        if (this->m_Name == p.m_Name)
        {
            return 1;
        }
        else
            return 0;
    }

    bool operator<(const Person& p)    //需要排序前提的算法一般需要重载<
    {
        return this->m_Age<p.m_Age ;
     } 

private:
    string m_Name;
    int m_Age;

    friend void test02();
    friend class MyPrint;
    friend class MyCompare;
    friend class MySearch;
    friend bool mySearch(const Person& p,const string& name);
};

Person::Person(string name, int age)
{
    this->m_Name = name;
    this->m_Age = age;
}

Person::~Person()
{

}

class MyPrint
{
public:
    void operator()(const Person& p)
    {
        cout << "姓名" << p.m_Name << "年龄" << p.m_Age << endl;
    }
};

class MyCompare
{
public:
    bool operator()(const Person& p1, const Person& p2)
    {
        return p1.m_Name > p2.m_Name;
    }
};

class MySearch
{
    public:
        bool operator()(const Person& p,const string& name)const
        {
            return p.m_Name==name;
        }
        
};

void test01()
{
    vector<int>v;
    for (int i = 0; i < 9; i++)
    {
        v.push_back(i);
    }

    int result=binary_search(v.begin(), v.end(), 6);
    if (result == 1)
    {
        cout << "已找到" << endl;
    }
    else
        cout << "未找到" << endl;
}

bool mySearch(const Person& p,const string& name)
{
    return p.m_Name==name;
}

void test02()
{
    vector<Person>v;
    char nameSeed[3][2] = { "A","B","C" };
    srand((unsigned int)time(NULL));

    for (int i = 0; i < 3; i++)//赋值
    {
        int age = rand() % 100 + 1;
        v.push_back(Person((const char*)nameSeed[i], age));
    }
    sort(v.begin(), v.end(), MyCompare());//先排序 
    for_each(v.begin(), v.end(), MyPrint()); cout << endl;


    Person pp("B", 0);//我不会看底层代码,觉得底层因该是用到==重载,我用对象pp去找v的第二个元素''B"
    int result = binary_search(v.begin(), v.end(),pp,MySearch());
    if (result == 1)
    {
        cout << "已找到" << endl;
    }
    else
        cout << "未找到" << endl;
}

int main()
{
    test01();
    test02();





}


  • 写回答

3条回答 默认 最新

  • Loup&卡普 2023-07-10 22:31
    关注

    binary_search 的谓语是定义查找规则,即 容器中任意两个元素谁大谁小,需要调用你的 compare

    //感觉不如使用 
    std::find_if(v.begin(),v.end(), [&pp](const Person&p) {return p.name==pp.name;});
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 7月12日
  • 已采纳回答 7月12日
  • 创建了问题 7月10日

悬赏问题

  • ¥15 软件供应链安全是跟可靠性有关还是跟安全性有关?
  • ¥15 电脑蓝屏logfilessrtsrttrail问题
  • ¥20 关于wordpress建站遇到的问题!(语言-php)(相关搜索:云服务器)
  • ¥15 【求职】怎么找到一个周围人素质都很高不会欺负他人,并且未来月薪能够达到一万以上(技术岗)的工作?希望可以收到写有具体,可靠,已经实践过了的路径的回答?
  • ¥15 Java+vue部署版本反编译
  • ¥100 对反编译和ai熟悉的开发者。
  • ¥15 带序列特征的多输出预测模型
  • ¥15 Python 如何安装 distutils模块
  • ¥15 关于#网络#的问题:网络是从楼上引一根网线下来,接了2台傻瓜交换机,也更换了ip还是不行
  • ¥15 资源泄露软件闪退怎么解决?