// map_sample.cpp : 定义控制台应用程序的入口点。
//
/*
function :
typedef std::map<string,string,uiCmp> setting;
使用ALT的C++标准库里面的函数,而不是自己写排序函数,for节约时间.
created : 2014-3-31 by dmd
*/
#include "stdafx.h"
#include <map>
#include <string>
#include <iostream>
using namespace std;
typedef struct tagStudentInfo
{
int nID;
string strName;
}StudentInfo, *PStudentInfo; //学生信息
class sort1
{
public:
bool operator() (StudentInfo const &_A, StudentInfo const &_B) const
{
if(_A.nID < _B.nID)
{
return true;
}
if(_A.nID == _B.nID)
{
return _A.strName.compare(_B.strName) < 0;
}
return false;
}
};
class sort2
{
public:
bool operator()(const string& str1,const string& str2)
{
return str1.compare(str2) < 0;
}
};
void test_map_sample1();
void test_map_sample2();
void test_map_sample3();
void test_map_sample4();
int main(int argc, _TCHAR* argv[])
{
test_map_sample1();
test_map_sample2();
test_map_sample3();
test_map_sample4();
cin.get();
return 0;
}
/*
map_student.insert(pair<int,string>(1,"student1"));
*/
void test_map_sample1()
{
cout<<" test : map_student.insert(pair<int,string>(1,stringvalue));"<<endl;
map<int,string> map_student;
map_student.insert(pair<int,string>(1,"student1"));
map_student.insert(pair<int,string>(1,"student3"));
map_student.insert(pair<int,string>(39,"student26"));
map_student.insert(pair<int,string>(41,"student9"));
map<int, string>::iterator iter;
cout<<" iter->first = int "<<endl;
cout<<" iter->second = string "<<endl;
for(iter = map_student.begin(); iter != map_student.end(); iter++)
{
cout<< iter->first <<" = "<< iter->second <<endl;
}
cout<<"如果int第一个关键子相同,第二次就不能插入相同关键字的变量"<<endl;
}
/*
map_student.insert(pair<int,string>(1,"student1"));
mapStudent.insert(map<int, string>::value_type (1, “student_one”));
*/
void test_map_sample2()
{
cout<<" test : map_student.insert(map<int,string>::value_type(11,string_value1));"<<endl;
map<int, string> map_student;
map_student.insert(map<int,string>::value_type(11,"stu_1"));
map_student.insert(map<int,string>::value_type(11,"stu_2"));
map_student.insert(map<int,string>::value_type(33,"stu_3"));
map_student.insert(map<int,string>::value_type(44,"stu_4"));
map<int,string>::iterator iter1;
for(iter1 = map_student.begin(); iter1!= map_student.end(); iter1++)
{
cout<< "int value:" <<iter1->first << " string value:"<<iter1->second<<endl;
}
cout<<"如果int第一个关键子相同,第二次就不能插入相同关键字的变量"<<endl;
}
/*
sort
*/
void test_map_sample3()
{
//用学生信息映射分数
cout<<"相同ID的时候,名字进行排序"<<endl;
map<StudentInfo, int, sort1>mapStudent;
StudentInfo studentInfo;
studentInfo.nID = 1;
studentInfo.strName = "BBB";
mapStudent.insert(pair<StudentInfo, int>(studentInfo, 65));
studentInfo.nID = 2;
studentInfo.strName = "DDD";
mapStudent.insert(pair<StudentInfo, int>(studentInfo, 80));
studentInfo.nID = 2;
studentInfo.strName = "CCC";
mapStudent.insert(pair<StudentInfo, int>(studentInfo, 80));
map<StudentInfo, int, sort1>::iterator iter;
int i=0;
for(iter= mapStudent.begin(); iter != mapStudent.end(); iter++)
{
//cout<<iter->first <<" :: "<<iter->second <<endl;
cout<<"NO :"<<i<<endl;
cout<< iter->first.nID <<endl;
cout<< iter->first.strName <<endl;
cout<< iter->second <<endl;
i++;
}
}
void test_map_sample4()
{
//用学生信息映射分数
cout<<"相同ID的时候,名字进行排序 4 ,此方法最简单"<<endl;
cout<<"从XML里面获得相同的Key得到的值进行排序"<<endl;
map<string, int, sort2> mapStudent;
mapStudent.insert(map<string, int>::value_type("stu_34",11));
mapStudent.insert(map<string, int>::value_type("stu_2",11));
mapStudent.insert(map<string, int>::value_type("stu_3",22));
mapStudent.insert(map<string, int>::value_type("stu_4",22));
map<string, int, sort2>::iterator iter1;
for(iter1 = mapStudent.begin(); iter1!= mapStudent.end(); iter1++)
{
cout<< "int value:" <<iter1->first << " string value:"<<iter1->second<<endl;
}
}
使用ALT的C++标准库里面的map
最新推荐文章于 2025-08-01 15:04:36 发布
