我们在VS2008环境下使用Windows控制台程序来测试本部分的相关代码,相关配置步骤如下:
第一步:打开VS2008 文件——新建——项目,出现以下窗口
第二步:点击确定,如下配置
单击下一步
单击完成,新建项目成功
(头文件 右键—添加—新建项—.h文件,源文件同理)
配置完成后,下面是代码示例:
//map_class.h文件:
<strong>#ifndef MAP_CLASS_H
#define MAP_CLASS_H
#include<map>
#include<string>
#include <stdio.h>
using namespace std;
class MapClass
{
public:
//构造函数
MapClass();
//析构函数
~MapClass();
//设置数据
bool SetMapData(string key, int value);
bool SetMapData(int key, string value);
//获取数据
int GetMapData(string key);
string GetMapData(int key);
//删除数据
bool RemoveMapData(string key);
bool RemoveMapData(int key);
//打印Map信息
void PrintMap();
private:
//[string]=int
map<string, int>MapString;
//[int]=string
map<int, string>MapInt;
//限制MapString容量大小
int MapStringMaxSize;
//限制MapInt容量大小
int MapIntMaxSize;
};
#endif
</strong>
//map_class.cpp文件
#include "map_class.h"
MapClass::MapClass()
{
MapStringMaxSize = 500;
MapIntMaxSize = 500;
}
MapClass::~MapClass()
{
if(!MapString.empty()){
for(map<string, int>::iterator iter = MapString.begin(); iter != MapString.end(); iter++){
MapString.erase(iter);
}
MapString.clear();
}
if(!MapInt.empty()){
for(map<int, string>::iterator iter = MapInt.begin(); iter != MapInt.end(); iter++){
MapInt.erase(iter);
}
MapInt.clear();
}
}
bool MapClass::SetMapData(string key, int value)
{
map<string ,int>::iterator iter;
iter=MapString.find(key);
if(iter != MapString.end()){
iter->second = value;
return true;
}
int size = MapString.size();
if(size >MapStringMaxSize){
return false;
}
MapString.insert(pair<string, int>(key, value));
return true;
}
bool MapClass::SetMapData(int key, string value)
{
map<int ,string>::iterator iter;
iter=MapInt.find(key);
if(iter != MapInt.end()){
MapInt.erase(iter);
}
else{
int size = MapInt.size();
if(size >MapIntMaxSize){
return false;
}
}
MapInt.insert(pair<int, string>(key, value));
return true;
}
int MapClass::GetMapData(string key)
{
map<string ,int>::iterator iter;
iter=MapString.find(key);
if(iter == MapString.end()){
return 0;
}
return iter->second;
}
string MapClass::GetMapData(int key)
{
map<int ,string>::iterator iter;
iter=MapInt.find(key);
if(iter == MapInt.end()){
return NULL;
}
return iter->second;
}
bool MapClass::RemoveMapData(string key)
{
map<string ,int>::iterator iter;
iter=MapString.find(key);
if(iter == MapString.end()){
return false;
}
MapString.erase(iter);
return true;
}
bool MapClass::RemoveMapData(int key)
{
map<int ,string>::iterator iter;
iter=MapInt.find(key);
if(iter == MapInt.end()){
return false;
}
MapInt.erase(iter);
return true;
}
void MapClass::PrintMap()
{
if(!MapString.empty()){
for(map<string, int>::iterator iter = MapString.begin(); iter != MapString.end(); iter++){
printf("MapString first:%s, second:%d \n", iter->first.c_str(), iter->second);
}
}
if(!MapInt.empty()){
for(map<int, string>::iterator iter = MapInt.begin(); iter != MapInt.end(); iter++){
printf("MapInt first:%d, second:%s \n", iter->first, iter->second.c_str());
}
}
}
//main函数的位置,可以放在本cpp文件里,也可以放在map_Main1文件里,系统会自动寻找。
//
//#include "stdafx.h"
//#include "map_class.h"
//using namespace std;
//
//
//int _tmain(int argc, _TCHAR* argv[])
//{
// MapClass* _mapObj = new MapClass();
// _mapObj->SetMapData("aaa", 1);
// _mapObj->SetMapData("bbb", 2);
// _mapObj->SetMapData(3, "ccc");
// _mapObj->SetMapData(4, "ddd");
// _mapObj->PrintMap();
//
// string ccc = _mapObj->GetMapData(3);
// int aaa = _mapObj->GetMapData("aaa");
// printf("\nccc:%s aaa:%d\n\n", ccc.c_str(), aaa);
// _mapObj->RemoveMapData("aaa");
// _mapObj->RemoveMapData(4);
// _mapObj->PrintMap();
//
// return 0;
//}
//
/*
05.C++ Maps是一种关联式容器,包含"关键字/值"对
06.Maps是没有扩充容量的,根据你的运行环境不同而不同,max_size()函数会告诉你在当前的机器上使用map的最大容量
07.map的基本操作函数:
08.begin() 返回指向map头部的迭代器
09.clear() 删除所有元素
10.count() 返回指定元素出现的次数
11.empty() 如果map为空则返回true
12.end() 返回指向map末尾的迭代器
13.equal_range() 返回特殊条目的迭代器对
14.erase() 删除一个元素
15.find() 查找一个元素
16.get_allocator() 返回map的配置器
17.insert() 插入元素
18.key_comp() 返回比较元素key的函数
19.lower_bound() 返回键值>=给定元素的第一个位置
20.max_size() 返回可以容纳的最大元素个数
21.rbegin() 返回一个指向map尾部的逆向迭代器
22.rend() 返回一个指向map头部的逆向迭代器
23.size() 返回map中元素的个数
24.swap() 交换两个map
25.upper_bound() 返回键值>给定元素的第一个位置
26.value_comp() 返回比较元素value的函数
*/
//mapMain1.cpp文件
//#include "stdafx.h"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include "map_class.h"
using namespace std;
int main()
{
MapClass* _mapObj = new MapClass();
_mapObj->SetMapData("aaa", 1);
_mapObj->SetMapData("bbb", 2);
_mapObj->SetMapData(3, "ccc");
_mapObj->SetMapData(4, "ddd");
_mapObj->PrintMap();
string ccc = _mapObj->GetMapData(3);
int aaa = _mapObj->GetMapData("aaa");
printf("\nccc:%s aaa:%d\n\n", ccc.c_str(), aaa);
_mapObj->RemoveMapData("aaa");
_mapObj->RemoveMapData(4);
_mapObj->PrintMap();
system("pause");
return 0;
}
注:STL中的map容器是我们经常用的,以下是map容器的三种插入方式:
//第一种:用insert函数插入pair数据。下面举例说明:
#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
map<int, string> mapStudent;
mapStudent.insert(pair<int, string>(1, "student_one"));
mapStudent.insert(pair<int, string>(2, "student_two"));
mapStudent.insert(pair<int, string>(3, "student_three"));
map<int, string>::iterator iter;
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
{
cout<<iter->first<<" "<<iter->second<<endl;
}
}
/*
//第二种:用insert函数插入value_type数据,下面举例说明:
#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
map<int, string> mapStudent;
mapStudent.insert(map<int, string>::value_type (1, "student_one"));
mapStudent.insert(map<int, string>::value_type (2, "student_two"));
mapStudent.insert(map<int, string>::value_type (3, "student_three"));
map<int, string>::iterator iter;
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
{
cout<<iter->first<<" "<<iter->second<<end;
}
}
//第三种:用数组方式插入数据,下面举例说明:
#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
map<int, string> mapStudent;
mapStudent[1] = "student_one";
mapStudent[2] = "student_two";
mapStudent[3] = "student_three";
map<int, string>::iterator iter;
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
{
cout<<iter->first<<" "<<iter->second<<end;
}
}
*/
/*
以上三种用法,虽然都可以实现数据的插入,但是它们是有区别的,当然了第一种和第二种在效果上是完成一样的,用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对应的值,用程序说明:
mapStudent.insert(map<int, string>::value_type (1, "student_one"));
mapStudent.insert(map<int, string>::value_type (1, "student_two"));
上面这两条语句执行后,map中1这个关键字对应的值是“student_one”,第二条语句并没有生效,那么这就涉及到我们怎么知道insert语句是否插入成功的问题了,可以用pair来获得是否插入成功,程序如下
pair<map<int, string>::iterator, bool> Insert_Pair;
Insert_Pair = mapStudent.insert(map<int, string>::value_type (1, "student_one"));
我们通过pair的第二个变量来知道是否插入成功,它的第一个变量返回的是一个map的迭代器,如果插入成功的话Insert_Pair.second应该是true的,否则为false。
*/
/*
//下面给出完成代码,演示插入成功与否问题:
#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
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>(1, "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<<end;
}
}
//大家可以用如下程序,看下用数组插入在数据覆盖上的效果
#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
map<int, string> mapStudent;
mapStudent[1] = "student_one";
mapStudent[1] = "student_two";
mapStudent[2] = "student_three";
map<int, string>::iterator iter;
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
{
cout<<iter->first<<" "<<iter->second<<end;
}
}
*/