天天看點

c++ map使用

最近學習中需要用到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;
}
           
c++ map使用
c++ map使用

繼續閱讀