当在STL应用中,我们常常会使用到结构体,这就需要我们对特定要求的运算符进行重载。例如在,STL中的排序都是默认使用小于号来排序。因此,在对结构体排序时,我们就需要重载小于号。
举例:
#include
#include
#include
using namespace std;
//学生信息
typedef struct tagStudentInfo
{
int nID;
string strName;
bool operator <(const tagStudentInfo &A) const
{
if (nID < A.nID) return true; //先比较nID
if (nID == A.nID) return strName.compare(A.strName) < 0; //nID相同时,再比较strName
return false;
}
}StudentInfo,*pstudentInfo;
int main()
{
//用学生信息映射分数
map mapStudent;
StudentInfo studentInfo;
studentInfo.nID = 1;
studentInfo.strName = "student_one";
mapStudent.insert(map::value_type(studentInfo,90));
studentInfo.nID = 2;
studentInfo.strName = "student_two";
mapStudent.insert(map::value_type(studentInfo,80));
studentInfo.nID = 2;
studentInfo.strName = "student_three";
mapStudent.insert(map::value_type(studentInfo,80));
map::iterator iter;
for (iter=mapStudent.begin(); iter!=mapStudent.end(); iter++)
cout << iter->first.nID << iter->first.strName << endl << iter->second << endl;
return 0;
}
tplink路由器默认密码是多少(tplink路由器默认密码详解)
tplink路由器默认密码怎么查看(tplink路由器默认密码查看方法)
tplink无线路由器密码怎么重置(tplink无线路由器密码重置方法)
手机如何设置tplink无线路由器密码和密码共享功能(手机设置tplink无线路由器密码和密码共享功能方法)
tplink无线路由器密码忘记了怎么办(tplink无线路由器密码忘记了解决方案)
tplogin路由器可以无线桥接么(TPLink路由器是否支持无线桥接功能)