标签:str lis char* map 没有 失效 name first window
map的erase windows和linux不同,而迭代器弄不好就失效
1 #include <iostream>
2 #include <map>
3 #include <string>
4
5 using namespace std;
6
7 int main(int argc, char* argv[])
8 {
9 map<string, int> MyMap;
10
11 MyMap["bujiwu"] = 26;
12 MyMap["swallow"] = 25;
13
14 std::cout <<"Before delete element:" << std::endl;
15 for( map<string, int>::iterator Itor = MyMap.begin(); Itor != MyMap.end(); Itor++ )
17 {
18 std::cout << Itor->first << " " << Itor->second << std::endl;
19 }
20
21 for( map<string, int>::iterator Itor = MyMap.begin(); Itor != MyMap.end(); )
23 {
24 if ( Itor->first == "bujiwu" )
25 {
26 MyMap.erase(Itor++); //在windows下也可以Itor = MyMap.erase(Itor),但是在linux下不行。
27 }
28 else
29 {
30 Itor++;
31 }
32 }
33
34 std::cout <<"After delete element:" << std::endl;
35 for( map<string, int>::iterator Itor = MyMap.begin(); Itor != MyMap.end(); Itor++ )
37 {
38 std::cout << Itor->first << " " << Itor->second << std::endl;
39 }
40
41 return 0;
42 }
通过测试在不管在linux还是Windows平台下,vector,list都有方法iterator erase( iterator _Where );
但是在Linux下map中没有方法iterator erase( iterator _Where ); 所以上述代码在Linux平台下第26行必须写成MyMap.erase(Itor++),而不能写能Itor = MyMap.erase(Itor). 在Windows下二种方法都可以。
标签:str lis char* map 没有 失效 name first window
原文地址:http://www.cnblogs.com/zzyoucan/p/7898697.html