STL map中key爲結構體的用法

最近在使用stl中的map容器時,碰到key爲結構體的情況,總結如下,以便提醒自己。
我的使用情景是,我需要根據不同的比例尺、道路類型這兩個參數獲取到對應的道路寬度,由於我是使用map解決這個問題的,
自然而然的就以比例尺、道路類型這兩個參數爲key,道路寬度爲value,建立的key如下:
1 typedef struct tagRoadKey 
2 {
3     int nType;
4     int nScale;
5     }
6 }ROADKEY;
但是編譯的時候,報了這個錯誤
d:\program files\microsoft visual studio\vc98\include\functional(86) : error C2784: 'bool __cdecl std::operator <(const class std::multimap<_K,_Ty,_Pr,_A> &,const class std::multimap<_K,_Ty,_Pr,_A> &)' : could not deduce template argument for 'const
 class std::multimap<_K,_Ty,_Pr,_A> &' from 'const struct tagRoadKey'

說實話,當初不太明白這是什麼錯誤,但從個人經驗來判斷,問題肯定出在這個key上面,後來google下,看到了別人寫的文章,才知道原因,原來map中的key默認是以less<>升序對元素排序(排序準則也可以修改),也就是說key必須具備operator<對元素排序,而平常我們的用的基本上都是基本類型元素作爲key,所以就不存在這個問題了,更詳細的解釋請看C++標準程序庫一書,第六章,set容器章節。
改正後的結構體如下:
 1 typedef struct tagRoadKey 
 2 {
 3     int nType;
 4     int nScale;
 5 
 6     bool operator <(const tagRoadKey& other) const
 7     {
 8         if (nType < other.nType)        //類型按升序排序
 9         {
10             return true;
11         }
12         else if (nType == other.nType)  //如果類型相同,按比例尺升序排序
13         {
14             return nScale < other.nScale;
15         }
16         
17         return false;
18     }
19 }ROADKEY;

完整代碼如下:
  1 //////////.h///////
  2 #ifndef _CROADWIDTHMNG_H_
  3 #define _CROADWIDTHMNG_H_
  4 #include <map>
  5 
  6 using namespace std;
  7 
  8 /*
  9 說明:根據當前比例尺、道路類型獲取對應的道路寬度
 10 */
 11 typedef struct tagRoadKey 
 12 {
 13     int nType;
 14     int nScale;
 15 
 16     bool operator <(const tagRoadKey& other) const
 17     {
 18         if (nType < other.nType)        //類型按升序排序
 19         {
 20             return true;
 21         }
 22         else if (nType == other.nType)  //如果類型相同,按比例尺升序排序
 23         {
 24             return nScale < other.nScale;
 25         }
 26         
 27         return false;
 28     }
 29 }ROADKEY;
 30 
 31 struct tagRoadInfo
 32 {
 33     tagRoadKey roadKey;
 34     int        nRoadWidth;
 35 };
 36 
 37 class CRoadWidthMng
 38 {
 39 public:
 40     CRoadWidthMng();
 41     virtual ~CRoadWidthMng();
 42 
 43 public:
 44     int GetRoadWidth(int nRoadType, int nScale); //根據道路類型、比例尺獲取寬度
 45 
 46 private:
 47     void Init();
 48 
 49 private:
 50     const int  MIN_SCALE;           //最小的比例尺
 51     const int  DEFAULT_ROAD_WIDTH;  //如沒有找到,返回默認值
 52 
 53     map<ROADKEY, int>m_roadMap;
 54 };
 55 
 56 #endif
 57 
 58 ////////.cpp///////
 59 #include "CRoadWidthMng.h"
 60 
 61 tagRoadInfo roadInfoItem[] =
 62 {
 63     ///////////高速公路//////////////
 64     {
 65         {
 66             10,
 67             12
 68         },
 69             16
 70     },
 71     {
 72         {
 73             10,
 74             11
 75         },
 76             12
 77     },
 78 
 79     {
 80         {
 81             10,
 82             10
 83         },
 84             6
 85     },
 86     {
 87         {
 88             10,
 89             9
 90         },
 91             3
 92     },
 93     ///////國道/////////////////
 94     {
 95         {
 96             12,
 97             12
 98         },
 99             12
100     },
101     {
102         {
103             12,
104             11
105         },
106             8
107     },
108 
109     {
110         {
111             12,
112             10
113         },
114             6
115     },
116     {
117         {
118             12,
119             9
120         },
121             4
122     },
123     ///////省道/////////////////
124     {
125         {
126             14,
127             12
128         },
129             10
130     },
131     {
132         {
133             14,
134             11
135         },
136             10
137     },
138 
139     {
140         {
141             14,
142             10
143         },
144             6
145     },
146     {
147         {
148             14,
149             9
150         },
151             4
152     },
153     ///////鐵路/////////////////
154     {
155         {
156             21,
157             12
158         },
159             1
160     },
161     {
162         {
163             21,
164             11
165         },
166             1
167     },
168 
169     {
170         {
171             21,
172             10
173         },
174             1
175     },
176     {
177         {
178             21,
179             9
180         },
181             1
182     },
183 };
184 
185 CRoadWidthMng::CRoadWidthMng()
186     :MIN_SCALE(6), DEFAULT_ROAD_WIDTH(5)
187 {
188     Init();
189 }
190 
191 CRoadWidthMng::~CRoadWidthMng()
192 {
193     m_roadMap.clear();
194 }
195 
196 void CRoadWidthMng:: Init()
197 {
198     int nNum = sizeof(roadInfoItem) / sizeof(roadInfoItem[0]);
199 
200     for (int i = 0; i < nNum; ++i)
201     {
202         m_roadMap.insert(make_pair(roadInfoItem[i].roadKey, roadInfoItem[i].nRoadWidth));
203     }
204 }
205 
206 int CRoadWidthMng:: GetRoadWidth(int nRoadType, int nScale)
207 {
208     if (nScale < MIN_SCALE)
209     {
210         nScale = MIN_SCALE;
211     }
212 
213     map<ROADKEY, int>::iterator itor;
214 
215     int nValue;
216     ROADKEY roadkey;
217     roadkey.nType = nRoadType;
218     roadkey.nScale = nScale;
219 
220     itor = m_roadMap.find(roadkey);
221 
222     if (itor != m_roadMap.end())
223     {
224         nValue =  itor->second/*nRoadWidth*/;
225     }
226     else
227     {
228         nValue =  DEFAULT_ROAD_WIDTH;
229     }
230 
231     return nValue;
232 }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章