聯合容器的第三個參數
map 容器的第三個參數,即函數對象類型。
對第一個參數進行比較,重載 operator ()。這是第一種方案。
第二種方案,對第一個參數類型進行包裝,然后針對這個類型重載 operator < 。
總結:
·第一個參數原裝類型,添加第三個類型,函數對象,重載 operator () 。
·第一個參數對原類型進行包裝,重載 operator < 。
參考:
為什么數據結構很重要
http://download.csdn.net/detail/yun_2106118/1768192
1 #include <iostream>
2 #include <map>
3 using namespace std;
4
5 struct ltstr
6 {
7 bool operator () (const char* s1, const char* s2) const
8 {
9 return strcmp(s1, s2) < 0;
10 }
11 };
12
13 int main()
14 {
15 map<const char*, const char*, ltstr> phones;
16 phones["Gao Jun"] = "23423423";
17 phones["Gao Jie"] = "89878979";
18
19 for (map<const char*, const char*, ltstr>::const_iterator cit = phones.begin(); cit != phones.end(); ++cit)
20 {
21 cout << cit->first << '\t' << cit->second << endl;
22 }
23
24 return 0;
25 }
posted on 2011-09-10 12:51
unixfy 閱讀(254)
評論(0) 編輯 收藏 引用