最近学习中需要用到stl map和vector,所以此处就整理了下,把它封装成一个类,方便自己以后使用,后面会加上vector的使用
map_class.h代码:
- #ifndef MAP_CLASS_H
- #define MAP_CLASS_H
-
- /*
- C++ Maps是一种关联式容器,包含"关键字/值"对
- Maps是没有扩充容量的,根据你的运行环境不同而不同,max_size()函数会告诉你在当前的机器上使用map的最大容量
- map的基本操作函数:
- begin() 返回指向map头部的迭代器
- clear() 删除所有元素
- count() 返回指定元素出现的次数
- empty() 如果map为空则返回true
- end() 返回指向map末尾的迭代器
- equal_range() 返回特殊条目的迭代器对
- erase() 删除一个元素
- find() 查找一个元素
- get_allocator() 返回map的配置器
- insert() 插入元素
- key_comp() 返回比较元素key的函数
- lower_bound() 返回键值>=给定元素的第一个位置
- max_size() 返回可以容纳的最大元素个数
- rbegin() 返回一个指向map尾部的逆向迭代器
- rend() 返回一个指向map头部的逆向迭代器
- size() 返回map中元素的个数
- swap() 交换两个map
- upper_bound() 返回键值>给定元素的第一个位置
- value_comp() 返回比较元素value的函数
- */
-
- #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
map_class.cpp代码:
- #include "map_calss.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());
- }
- }
- }
例子:
- // maptest.cpp : 定义控制台应用程序的入口点。
- //
-
- #include "stdafx.h"
- #include "map_calss.h"
-
-
- 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;
- }
|