Differences in implementation of Map-like erase methods in Linux and Windows

Source: Internet
Author: User
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 ();
16 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 ();
22 itor! = Mymap. End ();)
23 {
24 if (itor-> first = "bujiwu ")
25 {
26 mymap. Erase (itor ++); // in Windows, you can also use itor = mymap. Erase (itor), but not in 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 ();
36 itor! = Mymap. End (); itor ++)
37 {
38 STD: cout <itor-> first <"" <itor-> second <STD: Endl;
39}
40
41 return 0;
42}
43
44


The iterator method is available for vector and list on both Linux and Windows platforms.
Erase (iterator _ Where );

However, in Linux, map does not have the iterator erase (iterator _ Where) method );

Therefore, the above Code must be written as mymap in line 26th on Linux. erase (itor ++), but cannot write itor = mymap. erase (itor ). you can use either of the two methods in windows.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.