WeiHandsome 2023-06-29 10:12 采纳率: 85%
浏览 21
已结题

map自定数据怎么排序

img

我用了三个打印方法,然后插入了一些数据,但是没有输出,麻烦大家看看哪里出问题了
map<Person,int,MyCompare>
Person里面有年龄,我想按年龄排序;还有姓名
int是编号
MyCompare是伪函数

#include<iostream>
using namespace std;

#include<map>
#include<string>
#include<algorithm>
class MyCompare;
class Person
{
private:
    int m_Age;
    string m_Name;
public:
    Person(int age, string name)
    {
        this->m_Age = age;
        this->m_Name = name;
    }
    friend class  MyCompare;
    friend class PrintElement;
    friend void printElement(const pair<Person, int>& element);
    friend void showElement(const map<Person,int,MyCompare>& m);
};

/*
template <class T> 
class greater : binary_function <T,T,bool> 
{  
public:
  bool operator() (const T& x, const T& y) const  //常函数 
    {return x>y;}  
};  
*/
//仿函数排序
class  MyCompare
{
public:
    
    //bool operator()(int v1, int v2)
    //{
    //    return v1 > v2;
    //}

    bool operator()(const Person& p1,const Person& p2)const
    {
        return p1.m_Age > p2.m_Age;
    }

//    bool operator()(const pair<Person, int>& p1,const pair<Person, int>& p2)const
//    {
//        
//        return p1.first.m_Age > p2.first.m_Age;
//    }

};

//仿函数打印
class PrintElement
{
public:
    void operator()(const pair<Person, int>& element)
    {
        cout << element.first.m_Name<<","<< element.first.m_Age<< ", " << element.second << endl;
    }
};

// 普通函数打印
void printElement(const pair<Person, int>& element)
{
    cout << element.first.m_Name<<","<< element.first.m_Age<< ", " << element.second << endl;
}

//迭代器打印
void showElement(const map<Person,int,MyCompare>& m)
{

    for(map<Person,int,MyCompare>::const_iterator it=m.begin();it!=m.end();it++)
    {
        cout<<it->first.m_Name<<"\t"<<it->first.m_Age<<"\t"<<it->second<<endl;
    } 
    
}
int main()
{
    Person p[3] = { Person(18,"张三"),Person(19,"李四"),Person(20,"王五") };
    map<Person,int,MyCompare>m;
    for (int i = 0; i != 3; i++)
    {
        m.insert( make_pair(p[i],++i) );
    }
    
    for_each(m.begin(), m.end(), PrintElement());    
    for_each(m.begin(), m.end(), printElement);
    showElement(m);
    



}

  • 写回答

2条回答 默认 最新

  • 满衣兄 2023-06-29 10:47
    关注

    在你的代码中,存在几个问题:

    1、类 MyCompare 中的 operator() 方法应该是 const 成员函数,因为它不修改对象的状态。

    2、类 Person 中的友元类 MyCompare 应该在 Person 类之前进行声明。

    3、m.insert(make_pair(p[i], ++i)) 中的 ++i 是错误的。因为你在循环中使用了后缀递增运算符,导致每个元素的编号都是错误的。应该使用 i + 1。

    以下是修改后的代码:

    #include <iostream>
    #include <map>
    #include <string>
    #include <algorithm>
    using namespace std;
    
    class Person;  // 前向声明
    
    class MyCompare {
    public:
        bool operator()(const Person& p1, const Person& p2) const;
    };
    
    class Person {
    private:
        int m_Age;
        string m_Name;
    
    public:
        Person(int age, string name) {
            m_Age = age;
            m_Name = name;
        }
    
        friend class MyCompare;
        friend class PrintElement;
        friend void printElement(const pair<Person, int>& element);
        friend void showElement(const map<Person, int, MyCompare>& m);
    };
    
    bool MyCompare::operator()(const Person& p1, const Person& p2) const {
        return p1.m_Age > p2.m_Age;
    }
    
    class PrintElement {
    public:
        void operator()(const pair<Person, int>& element) {
            cout << element.first.m_Name << ", " << element.first.m_Age << ", " << element.second << endl;
        }
    };
    
    void printElement(const pair<Person, int>& element) {
        cout << element.first.m_Name << ", " << element.first.m_Age << ", " << element.second << endl;
    }
    
    void showElement(const map<Person, int, MyCompare>& m) {
        for (map<Person, int, MyCompare>::const_iterator it = m.begin(); it != m.end(); it++) {
            cout << it->first.m_Name << "\t" << it->first.m_Age << "\t" << it->second << endl;
        }
    }
    
    int main() {
        Person p[3] = { Person(18, "张三"), Person(19, "李四"), Person(20, "王五") };
        map<Person, int, MyCompare> m;
        for (int i = 0; i < 3; i++) {
            m.insert(make_pair(p[i], i + 1));
        }
    
        for_each(m.begin(), m.end(), PrintElement());
        for_each(m.begin(), m.end(), printElement);
        showElement(m);
    
        return 0;
    }
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 7月8日
  • 已采纳回答 6月30日
  • 创建了问题 6月29日

悬赏问题

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