stl之map 排序
排序问题,STL中默认是采用小于号来排序的,因为设置int等类型做key,它本身支持小于号运算,在一些特殊情况,比如关键字是一个结构体,涉及到排序就会出现问题,因为它没有小于号操作,insert等函数在编译的时候过不去,下面给出两个方法解决这个问题:
第一种:小于号重载,程序举例
#include <map>
#include <string>
using namespace std;
typedef struct tagStudentInfo
{
int nID;
string strName;
}StudentInfo, *PStudentInfo; //学生信息 int main()
{
int nSize; //用学生信息映射分数
map<StudentInfo, int>mapStudent;
map<StudentInfo, int>::iterator iter;
StudentInfo studentInfo;
studentInfo.nID = ;
studentInfo.strName = “student_one”;
mapStudent.insert(pair<StudentInfo, int>(studentInfo, ));
studentInfo.nID = ;
studentInfo.strName = “student_two”; mapStudent.insert(pair<StudentInfo, int>(studentInfo, ));
for (iter=mapStudent.begin(); iter!=mapStudent.end(); iter++)
cout<<iter->first.nID<<endl<<iter->first.strName<<endl<<iter->second<<endl;
}
//以上程序是无法编译通过的,只要重载小于号,就OK了,如下: typedef struct tagStudentInfo
{
int nID;
string strName;
Bool operator < (tagStudentInfo const& _A) const
{
//这个函数指定排序策略,按nID排序,如果nID相等的话,按strName排序
if(nID < _A.nID) return true;
if(nID == _A.nID) return strName.compare(_A.strName) < ;
return false;
}
}StudentInfo, *PStudentInfo; //学生信息
第二种:仿函数的应用,这个时候结构体中没有直接的小于号重载,程序说明
#include <map>
#include <iostream>
#include <string>
using namespace std; typedef struct tagStudentInfo
{
int nID;
string strName;
}StudentInfo, *PStudentInfo; //学生信息 class sort{
public:
bool operator() (StudentInfo const & _A, StudentInfo const & _B) const
{
if(_A.nID < _B.nID){
return true;
}else if (_A.nID == _B.nID){
return _A.strName.compare(_B.strName) < ;
}
return false;
}
}; int main()
{
int nSize; //用学生信息映射分数
map<StudentInfo, int, sort> mapStudent;
StudentInfo studentInfo;
studentInfo.nID = ;
studentInfo.strName = "student_one"; mapStudent.insert(pair<StudentInfo, int>(studentInfo, ));
studentInfo.nID = ;
studentInfo.strName = "tudent_two";
mapStudent.insert(pair<StudentInfo, int>(studentInfo, )); std::map<StudentInfo, int, sort>::iterator it;
for(it = mapStudent.begin(); it != mapStudent.end(); it++){
std::cout << it->second << std::endl;
}
}
stl之map 排序的更多相关文章
- STL之map排序
描述 STL的map中存储了字符串以及对应出现的次数,请分别根据字符串顺序从小到大排序和出现次数从小到大排序. 部分代码已经给出,请补充完整,提交时请勿包含已经给出的代码. int main() { ...
- STL对map排序
// sort start typedef struct{ ... }Node; // Map的键是字符串,值是结构体.虽然有自动排序特性,但是按字符串的排序并不能符合要求.此时,Map的key可以视 ...
- C++ STL中Map的按Key排序和按Value排序
map是用来存放<key, value>键值对的数据结构,可以很方便快速的根据key查到相应的value.假如存储学生和其成绩(假定不存在重名,当然可以对重名加以区 分),我们用map来进 ...
- C++ STL中Map的相关排序操作:按Key排序和按Value排序 - 编程小径 - 博客频道 - CSDN.NET
C++ STL中Map的相关排序操作:按Key排序和按Value排序 - 编程小径 - 博客频道 - CSDN.NET C++ STL中Map的相关排序操作:按Key排序和按Value排序 分类: C ...
- STL容器——对map排序
STL容器(三)——对map排序 对于map的排序问题,主要分为两部分:根据key排序:根据value排序.下面我们就分别说一下~ 1. 根据key进行排序 map默认按照key进行升序排序 ,和输入 ...
- C++ STL中Map的按Key排序跟按Value排序
C++ STL中Map的按Key排序和按Value排序 map是用来存放<key, value>键值对的数据结构,可以很方便快速的根据key查到相应的value.假如存储学生和其成绩(假定 ...
- 对vector等STL标准容器的排序操作
[+] STL提供的Sort 算法 所有sort算法介绍 sort 中的比较函数 sort 的稳定性 全排序 局部排序 nth_element 指定元素排序 partition 和stable_par ...
- STL中map与hash_map的比较
1. map : C++的STL中map是使用树来做查找算法; 时间复杂度:O(log2N) 2. hash_map : 使用hash表来排列配对,hash表是使用关键字来计算表位置; 时间复杂度:O ...
- 关于STL的map的注意事项
关于map是什么,这里就不多叙述了. 直接正题,常用的map插入操作有三种方法:通过pair<key_type,value_type>.通过value_type插入数据.还有一种类似于数组 ...
随机推荐
- CentOS Linux 语言环境设置
程序运行使用一套语言需要有字符集(数据)和字体(显示),Locale是根据计算机用户所使用的语言,所在国家或者地区,以及当地的文化传统所定义的一个软件运行时的语言环境. 一.locale详解 在 Li ...
- hdu 1078(记忆化搜索)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1078 //dp[i][j]表示从点i,j处开始能获得的最多cheese #include <io ...
- JAVA之数组查询binarySearch()方法详解
binarySearch()方法提供了多种重载形式,用于满足各种类型数组的查找需要,binarySearch()有两种参数类型 注:此法为二分搜索法,故查询前需要用sort()方法将数组排序,如果数组 ...
- javascript第六课类型转换
1.parseint(参数): 转换为整数,即使参数中的字符串包含字母数字混合,此方法也会自动一个一个判断和转换 parseInt(参数,进制);将参数通过几进制的方式转为数字 2.parsefl ...
- 最简单的视音频播放演示样例4:Direct3D播放RGB(通过Texture)
===================================================== 最简单的视音频播放演示样例系列文章列表: 最简单的视音频播放演示样例1:总述 最简单的视音频 ...
- [置顶] android利用jni调用第三方库——第三篇——编写库android程序整合第三方库libhello.so到自己的库libhelloword.so
0:前言: 在第二篇中,我们主要介绍了丙方android公司利用乙方C++公司给的动态库,直接调用库中的方法,但是这样方式受限于: 乙方C++公司开发的动态库是否符合jni的规范,如果不规范,则不能直 ...
- C# winfrom中Flash播放使用axShockwaveFlash控件设置透明XP出现白色背景解决办法,仿QQ魔法表情效果
//播放时 图片周围有锯齿白边出现 反锯齿处理暂无解决办法. 如有大神 请给我留言 新Form AllowDrop True 引用using System.IO; 拖1个Button p ...
- oracle丢失temp表空间处理
之前有做临时表空间的切换,切换后没drop tablespace就删除了temp01.dbf结果排序跟查dba_temp_files报错 SQL Mbytes from dba_temp_files; ...
- C#:占位符的例子
在c#中有两种方式可以输出多个字符. static void Main() { string c=Console.ReadLine(); string d=Console.ReadLine(); Co ...
- Java代码整理