由於使用map<char *,int> 表示的是指針值到int的映射,在實際使用中經常想表示的是指針內容與int的映射,而又不想使用map<string,int>,怎么辦?
可通過重載操作符實現
struct ptrCmp
{
bool operator()( const char * s1, const char * s2 ) const
{
return strcmp( s1, s2 ) < 0;
}
};
map<char *, int, ptrCmp> mapStr;
再使用就可以了。
但是需要注意,實際記錄的仍是char *地址到int的映射,查詢時變為了char *內容的大小比較,所以當鍵char *地址對應的內容發生變化時,也就相當於mapStr對應的鍵內容發生變化,故在使用時,需將mapStr的鍵值綁定到內容不會變化的地址。
可比較如下三個例子:(正確使用方法直接見代碼3)
代碼1:不使用ptrCmp

1 #include<stdlib.h> 2 #include<stdio.h> 3 #include<string.h> 4 #include<map> 5 using namespace std; 6 7 map<char *,int> mapStr; 8 char aa[1000][100]; 9 10 int main() 11 { 12 char temp[100]; 13 int ind=100; 14 int id=0; 15 for(int i=1;i<5;++i) 16 { 17 memset(temp,0,sizeof(temp)); 18 gets(temp); 19 if(mapStr.count(temp)<=0) 20 { 21 --ind; 22 strcpy(aa[ind],temp); 23 mapStr[aa[ind]]=ind; 24 id=ind; 25 } 26 else 27 { 28 id=mapStr[temp]; 29 } 30 printf("key:%s value:%d\n",temp,id); 31 } 32 printf("mapStr content:\n"); 33 int i=0; 34 for(map<char *,int>::iterator iter=mapStr.begin();iter!=mapStr.end();++iter) 35 { 36 ++i; 37 printf("%d. key:%s value:%d\n",i,iter->first,iter->second); 38 } 39 return 0; 40 }
輸入:
bb
aa
cc
bb
輸出:
key:bb value:99
key:aa value:98
key:cc value:97
key:aa value:96
mapStr content:
1. key:aa value:96
2. key:cc value:97
3. key:aa value:98
4. key:bb value:99
在查詢插入中,mapStr進行的是char*指針本身值的大小比較,而非指針指向內容的比較。mapStr按指針值大小而非指針指向內容的大小順序排放
代碼2:mapStr中鍵指向內容變化時

1 #include<stdlib.h> 2 #include<stdio.h> 3 #include<string.h> 4 #include<map> 5 using namespace std; 6 7 struct ptrCmp 8 { 9 bool operator()( const char * s1, const char * s2 ) const 10 { 11 return strcmp( s1, s2 ) < 0; 12 } 13 }; 14 map<char *,int,ptrCmp> mapStr; 15 char aa[1000][100]; 16 17 int main() 18 { 19 char temp[100]; 20 int ind=0; 21 int id=0; 22 for(int i=1;i<5;++i) 23 { 24 memset(temp,0,sizeof(temp)); 25 gets(temp); 26 if(mapStr.count(temp)<=0) 27 { 28 ++ind; 29 strcpy(aa[ind],temp); 30 mapStr[temp]=ind;//當temp內容發生變化時,該鍵值對對應鍵指向內容也發生了變化 31 id=ind; 32 } 33 else 34 { 35 id=mapStr[temp]; 36 } 37 printf("key:%s value:%d\n",temp,id); 38 } 39 printf("mapStr content:\n"); 40 int i=0; 41 for(map<char *,int,ptrCmp>::iterator iter=mapStr.begin();iter!=mapStr.end();++iter) 42 { 43 ++i; 44 printf("%d. key:%s value:%d\n",i,iter->first,iter->second); 45 } 46 return 0; 47 }
輸入:
bb
aa
cc
aa
輸出:
key:bb value:1
key:aa value:1
key:cc value:1
key:aa value:1
mapStr content:
1. key:aa value:1
在這種情況下,mapStr中僅記住了最后一次鍵指向內容更改的結果
代碼3:正確使用

1 #include<stdlib.h> 2 #include<stdio.h> 3 #include<string.h> 4 #include<map> 5 using namespace std; 6 7 struct ptrCmp 8 { 9 bool operator()( const char * s1, const char * s2 ) const 10 { 11 return strcmp( s1, s2 ) < 0; 12 } 13 }; 14 map<char *,int,ptrCmp> mapStr; 15 char aa[1000][100]; 16 17 int main() 18 { 19 char temp[100]; 20 int ind=0; 21 int id=0; 22 for(int i=1;i<5;++i) 23 { 24 memset(temp,0,sizeof(temp)); 25 gets(temp); 26 if(mapStr.count(temp)<=0) 27 { 28 ++ind; 29 strcpy(aa[ind],temp); 30 mapStr[aa[ind]]=ind; 31 id=ind; 32 } 33 else 34 { 35 id=mapStr[temp]; 36 } 37 printf("key:%s value:%d\n",temp,id); 38 } 39 printf("mapStr content:\n"); 40 int i=0; 41 for(map<char *,int,ptrCmp>::iterator iter=mapStr.begin();iter!=mapStr.end();++iter) 42 { 43 ++i; 44 printf("%d. key:%s value:%d\n",i,iter->first,iter->second); 45 } 46 return 0; 47 }
輸入:
bb
aa
cc
aa
輸出:
key:bb value:1
key:aa value:2
key:cc value:3
key:aa value:2
mapStr content:
1. key:aa value:2
2. key:bb value:1
3. key:cc value:3
可以看出,mapStr是按鍵char *的內容遞增順序存放的