C++工作笔记-map中结构体的比较
生活随笔
收集整理的這篇文章主要介紹了
C++工作笔记-map中结构体的比较
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
原理,重載小于符號。
運行截圖如下:
代碼如下:
//map的重載、排序#include <map> #include <string> #include <iostream> #include <time.h> using namespace std;typedef struct tagStudentInfo{int nID;string strName;bool operator<(tagStudentInfo const &_A)const{if (nID < _A.nID)return true;if (nID == _A.nID)return strName.compare(_A.strName) < 0;return false;} }StudentInfo, *PStudentInfo;int main(){//學生信息映射分數map<StudentInfo, int>mapStudent;map<StudentInfo, int>::iterator iter;StudentInfo studentInfo;studentInfo.nID = 1;studentInfo.strName = "小明";mapStudent.insert(pair<StudentInfo, int>(studentInfo, 90));studentInfo.nID = 2;studentInfo.strName = "小紅";mapStudent.insert(pair<StudentInfo, int>(studentInfo, 80));for (iter = mapStudent.begin(); iter != mapStudent.end(); iter++)cout << iter->first.nID << " " << iter->first.strName << " " << iter->second << endl;cout << endl;getchar();return 0; }總結
以上是生活随笔為你收集整理的C++工作笔记-map中结构体的比较的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++|Qt工作笔记-对explicit
- 下一篇: Linux学习笔记-使用gdb调试