ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

map自定义结构排序

2026/8/8 0:01:33 拓冰建站 浏览量
map自定义结构排序

我最近在我写代码的时候碰到map<自定义结构体, int >,对自定义结构的值的排序问题, 我解决问题之后,觉得还是写测试代码来讲清楚,方便C++的初学者。

 map<ThingProd, int >       mapThingProdFee 中ThingProd是自定义结构体,自定义结构体有三个值:int m_iName;int m_iAuthor;int m_iPrice;本代码主要讲如果对自定义结构体有三个值时的排序重定义。

#include <iostream>
#include <stdio.h>
#include <map>
using namespace std;typedef struct ThingProd
{ThingProd(){m_iName = 0;m_iAuthor = 0;m_iPrice = 0;}ThingProd(int iName, int iAuthor, int iPrice){m_iName = iName;m_iAuthor = iAuthor;m_iPrice = iPrice;}void clear(){m_iName = 0;m_iAuthor = 0;m_iPrice = 0;}bool operator <(const ThingProd& other) const{cout << "operator<" << endl;if (m_iAuthor < other.m_iAuthor)      //事务创作者编号越小越优先{printf("m_iAuthor=%d,other.m_iAuthor=%d return true\n", m_iAuthor, other.m_