由于使用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 *的内容递增顺序存放的