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);
    



}

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

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、类 MyCompare 中的 operator() 方法应该是 const 成员函数,因为它不修改对象的状态.
是要必须加const变成常函数吗?
不用,加const防止误操作,可以不加。

2、类 Person 中的友元类 MyCompare 应该在 Person 类之前进行声明。这里比较疑惑,类的先后顺序为啥是这样,Person的友元类还有PrintElementt,还有一些打印函数他们要在 Person 类之前进行声明吗?
class MyCompare;
class PrintElement;
这两个类需要声明,因为写友元的时候提到了他们俩。
函数不用声明,只有函数在main函数之后,才需要声明。

3、m.insert(make_pair(p[i], ++i)) 中的 ++i 是错误的。因为你在循环中使用了后缀递增运算符,导致每个元素的编号都是错误的。应该使用 i + 1。
为啥不可以写++i?
++i就是i=i+1,
for(int i=0;i<3;i++)
{
  m.insert(make_pair(p[i],++i));
  //++i;第一次循环序号是1,但i一下加了2次,下一次循环序号直接变成3了,既导致序号错误又差错了位置。
}
*/