聯(lián)合容器的第三個(gè)參數(shù)
map 容器的第三個(gè)參數(shù),即函數(shù)對(duì)象類型。
對(duì)第一個(gè)參數(shù)進(jìn)行比較,重載 operator ()。這是第一種方案。
第二種方案,對(duì)第一個(gè)參數(shù)類型進(jìn)行包裝,然后針對(duì)這個(gè)類型重載 operator < 。
總結(jié):
·第一個(gè)參數(shù)原裝類型,添加第三個(gè)類型,函數(shù)對(duì)象,重載 operator () 。
·第一個(gè)參數(shù)對(duì)原類型進(jìn)行包裝,重載 operator < 。
參考:
為什么數(shù)據(jù)結(jié)構(gòu)很重要
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 閱讀(258)
評(píng)論(0) 編輯 收藏 引用