c++中map容器简单功能实现测试附带结构体指针测试

// 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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值