// TestingandTobe.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<iostream>
#include<map>
#include<string>
using namespace std;
struct student{
// virtual void fun(){ 指针指向未知崩溃
void fun(){
m_i=0;
cout<<"结构体指针调用结构体中的函数测试,...m_i="<<m_i<<endl;
}
static int m_i;
};
int student::m_i=0;
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;
if(_A.nID == _B.nID) return _A.strName.compare(_B.strName) < 0;
return false;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
/*************
测试函数指针
*************/
student *m_p=0;
m_p->fun();
/*************
测试map容器
**************/
//数组方式插入数据
map<int,string> mapStudent;
mapStudent[3]="student one";
mapStudent[1]="student two";
mapStudent[2]="student three";
map<int,string>::iterator iter;
for(iter=mapStudent.begin();iter!=mapStudent.end();iter++)
{
cout<<"insert test..."<<iter->second<<endl;
}
//测试查找
map<int,string>::iterator iterr;
iterr =mapStudent.lower_bound(2);
{
cout<<"FindTest..."<<iterr->second<<endl;
}
iterr =mapStudent.upper_bound(2);
{
cout<<"FindTest..."<<iterr->second<<endl;
}
pair<map<int,string>::iterator,map<int,string>::iterator>mappair;
mappair =mapStudent.equal_range(2);
if(mappair.first==mappair.second)
{
cout<<"donot find"<<endl;
}else
{
cout<<"FindTest...find"<<endl;
}
//insert函数插入数据
/*map<int, string> mapStudent;
pair<map<int,string>::iterator,bool> Insert_Pair;
Insert_Pair=mapStudent.insert(pair<int, string>(1,"student one"));
if (Insert_Pair.second == true)
{
cout<<"Insert Successfully"<<endl;
}
else
{
cout<<"Insert Failure"<<endl;
}
Insert_Pair=mapStudent.insert(pair<int, string>(2,"student two"));
if(Insert_Pair.second == true)
{
cout<<"Insert Successfully"<<endl;
}
else
{
cout<<"Insert Failure"<<endl;
}
map<int, string>::iterator iter;
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
{
cout<<iter->first<<" "<<iter->second<<endl;
}
*/
/**************************
测试排序
***************************/
//用学生信息映射分数
int nSize;
map<StudentInfo, int, sort>mapStudentt ;
map<StudentInfo, int, sort>::iterator iterrr;
StudentInfo studentInfo;
studentInfo.nID = 1;
studentInfo.strName = "student_one";
mapStudentt.insert(pair<StudentInfo, int>(studentInfo, 90));
studentInfo.nID = 2;
studentInfo.strName = "student_two";
mapStudentt.insert(pair<StudentInfo, int>(studentInfo, 80));
for(iterrr=mapStudentt.begin();iterrr!=mapStudentt.end();iterrr++)
cout<<"Test order..."<<iterrr->second<<endl;
return 0;
}
c++中map容器简单功能实现测试附带结构体指针测试
最新推荐文章于 2024-04-08 20:39:32 发布