轉自於http://www.cnblogs.com/anywei/archive/2011/10/27/2226830.html
C++中map容器提供一個索引值對容器,map與multimap差別僅僅在於multiple允許一個鍵對應多個值。
一、map的說明
1 標頭檔
#include <map>
2 定義
map<string, int> my_Map;
或者是typedef map<string, int> MY_MAP;
MY_MAP my_Map;
3 插入資料
(1) my_Map["a"] = 1;
(2) my_Map.insert(map<string, int>::value_type("b",2));
(3) my_Map.insert(pair<string,int>("c",3));
(4) my_Map.insert(make_pair<string,int>("d",4));
4 尋找資料和修改資料
(1) int i = my_Map["a"];
my_Map["a"] = i;
(2) MY_MAP::iterator my_Itr;
my_Itr.find("b");
int j = my_Itr->second;
my_Itr->second = j;
不過注意,鍵本身是不能被修改的,除非刪除。
5 刪除資料
(1) my_Map.erase(my_Itr);
(2) my_Map.erase("c");
還是注意,第一種情況在迭代期間是不能被刪除的,道理和foreach時不能刪除元素一樣。
6 迭代資料
for (my_Itr=my_Map.begin(); my_Itr!=my_Map.end(); ++my_Itr) {}
7 其它方法
my_Map.size() 返回元素數目
my_Map.empty() 判斷是否為空白
my_Map.clear() 清空所有元素
可以直接進行賦值和比較:=, >, >=, <, <=, != 等等
更進階的應用查協助去吧,^_^;
二/map的舉例
要求: 將mymap中itemstruct 的a大於100的項刪除
struct itemstruct
{
int a;
char b[20];
};
map<string, itemstruct > mymap.
解答1:
#include <iostream>
#include <ctime>
#include <map>
using namespace std;
typedef struct itemstruct
{
int a;
char b[20];
}itemS;
itemS s[4] = {{102,"what"},
{33, "hello"},
{198,"world"},
{45, "c++"}
};;
int main()
{
map<string, itemS> mymap;
string str[4] = {"1st","2nd","3rd","4th"};
for(int i = 0; i<4; i++)
{
mymap.insert(make_pair(str[i], s[i]));
}
map<string,itemS>::iterator it;
for(it=mymap.begin(); it!=mymap.end(); it++)
{
if(it->second.a >100){
i=mymap.erase(it); ----->正確
mymap.erase(it); ----->it失效..
}
}
//first是Key, second是value;
for(it = mymap.begin(); it!=mymap.end(); it++)
{
cout<<it->first<<" "<<it->second.a<<" "<<it->second.b<<endl;
}
system("pause");
return 0;
}
解答2:
#include<map>
#include<iterator>
#include<string>
#include<iostream>
#include<cstring>
using namespace std;
struct itemstruct
{
int a;
char b[20];
itemstruct(int t,char*str)
{
a=t;
strcpy(b,str);
}
};
int main()
{
map<string,itemstruct>mymap;
mymap.insert(make_pair("a",itemstruct(10,"hanzhou")));
mymap.insert(make_pair("ab",itemstruct(20,"fuzhou")));
mymap.insert(make_pair("abc",itemstruct(30,"zhengzhou")));
mymap.insert(make_pair("abcd",itemstruct(200,"wuhan")));
mymap.insert(make_pair("abcde",itemstruct(150,"kunming")));
mymap.insert(make_pair("abcdef",itemstruct(50,"xiamen")));
map<string,itemstruct>::iterator it=mymap.begin();
while(it!=mymap.end())
{
if((it->second).a>100)mymap.erase(it++);
else it++;
}
it=mymap.begin();
while(it!=mymap.end())
{
cout<<it->first<<" "<<(it->second).a<<" "<<(it->second).b<<endl;
it++;
}
system("PAUSE");
return 0;
}
解答3:
for(map<string, itemstruct>::iterator i = mymap.begin(); i != mymap.end();)
{
if(i->second.a > 100)
i = mymap.erase(i);
else
++i;
}
解答4: VC6中編譯map編譯出錯的解決方案
Warnings similar to the following are generated even if you use the warning pragma to disable the warning: warning C4786: 'std::rb_tree<CAiSpanningTree<State,std::less<State>>::TransClosureNode, CAiSpanningTree<State,std::less<State>>::TransClosureNode,std::ident<Cai
SpanningTree<State,std::less<State>>::TransClosureNode,CAiSpanningTree<S tate,std::less<State>>::TransClosureNode>,std::less<CAiSpanningTree<Stat e,std::less<State>>::TransClosureNode>>' : identifier was truncated to '255' characters in the debug information
解決code 加在stdafx.h的標頭檔處:
#pragma warning(disable:4786)
以下轉自http://hi.baidu.com/men4661273/blog/item/a2036219efc32a1d34fa4148.html
Map集合排序
Map集合不同於list和set,Map是一個以索引值對儲存資料的集合,所以他的排序分為鍵排序和值排序
Map集合的鍵排序:
TreeMap自動按照鍵進行排序,對hashmap進行key排序可使用TreeMap的一個重載的構造方法new TreeMap(hashMap)即可
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("zhangsan", 200);
map.put("lisi", 150);
map.put("wangwu", 800);
map.put("liuliu", 366);
//排序前
System.out.println("排序前的map"+map);
Map<String, Integer> treeMap = new TreeMap<String, Integer>(map); //使用TreeMap重載的構造方法構造一個treeMap
System.out.println("排序後的map"+treeMap);
自訂TreeMap中鍵的排序方式,通過自己定義的比較子來實現,同樣是藉助Compatator,其泛型為TreeMap鍵的資料類型,可以顛倒排序的順序:
Map<String,Integer> treeMap = new TreeMap<String, Integer>(new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return o2.compareTo(o1);
}
});
treeMap.put("a", 33);
treeMap.put("b", 22);
treeMap.put("k", 66);
treeMap.put("f", 55);
treeMap.put("g", 11);
System.out.println(treeMap);
Map集合的值排序:
public class MapValueSort implements Comparator<Map.Entry<String, Integer>>{//值排序器
@Override
public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
return o2.getValue() - o1.getValue();
}
}
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("zhangsan", 200);
map.put("lisi", 150);
map.put("wangwu", 800);
map.put("liuliu", 366);
System.out.println("原汁原味map"+map);
//使用ArrayList重載的構造方法,構造一個list,將map集合放入該list
List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(map.entrySet());
//排序前
System.out.println("-複製到list"+map);
//排序 實質上是使用list的排序方法二來進行排序的
//自訂Comparator,裡面定義排序的屬性為按照value排序
MapValueSort mapValueSort = new MapValueSort();//執行個體化一個值排序的排序器
Collections.sort(list, mapValueSort);
//排序後
System.out.println("排序後的map"+list);