关于一段测试代码的知识点补习

 1 #include <iostream>
 2 #include <string>
 3 #include <set>
 4 #include <map>
 5 #include<vector>
 6 using namespace std;
 7 
 8 int main()
 9 {
10     set<int> set1;
11     set1.insert(5);
12     set1.insert(3);
13     set1.insert(6);
14     set1.insert(5);
15     for(set<int>::iterator set_ite = set1.begin();set_ite != set1.end();set_ite++)
16     {
17         cout << *set_ite<<" ";
18     }
19     cout<<endl;
20     
21     map<int,string> map1;
22     map1.insert(pair<int,string>(1,"master"));
23     //map1.insert(pair<int,string>(1,"docter"));
24     //map1[1] = "docter";
25     map<int,string>::iterator ite1 = map1.begin();
26     ite1->second = "docter";
27     map1.insert(pair<int,string>(4,"gay luo"));
28     map1.insert(pair<int,string>(3,"gay er"));
29     map1.insert(pair<int,string>(2,"son lin"));
30     map1.insert(pair<int,string>(-1,"son"));
31     for(map<int,string>::iterator ite1 = map1.begin(); ite1 != map1.end();ite1++)
32     {
33         cout<<ite1->first<<":"<<ite1->second<<endl;
34     }
35     cout<<endl;
36     //map<int,string>::iterator ite1 = map1.begin();
37     //ite1->second = "c++";
38     map<string,double> score;
39     score.insert(pair<string,double>("zhang san",92.5));
40     score.insert(pair<string,double>("li si",97));
41     score.insert(pair<string,double>("wang wu",90.5));
42    for(map<string,double>::iterator ite1 = score.begin(); ite1 != score.end();ite1++)
43     {
44         cout<<ite1->first<<":"<<ite1->second<<endl;
45     }
46     cout<<endl;
47     score["zhao liu"] = 93.5;
48     for(map<string,double>::iterator ite1 = score.begin(); ite1 != score.end();ite1++)
49     {
50         cout<<ite1->first<<":"<<ite1->second<<endl;
51     }
52     cout<<endl;
53     //vector test
54     int a[] = {1,2,3,4,5};
55     //cout<<"a'size:"<<sizeof(a)/sizeof(int)<<endl;
56     vector<int> vec1(a,a+sizeof(a)/sizeof(int));
57     
58     for(vector<int>::iterator ite1 = vec1.begin();ite1 < vec1.end();ite1++)
59     {
60         cout <<*ite1<<" ";
61     }
62     cout<<endl;
63    return 0;
64 }

这段代码给出了set ,map ,vector三种容器的插入和访问方式,具体不再赘述,现在说说遗漏的一些知识点:

知识点1:

1     int a[] = {1,2,3,4,5};
2     cout<<"a'size:"<<sizeof(a)/sizeof(int)<<endl;

sizeof()并不是函数,也是运算符!sizeof(数组名)得到的并不是数组的元素个数,而是所有元素的内存长度,需要除以类型的长度,才为元素个数。

知识点2:

 1     for(set<int>::iterator set_ite = set1.begin();set_ite != set1.end();set_ite++)
 2     {
 3         cout << *set_ite<<" ";
 4     }
 5     cout<<endl;
 6     
 7     for(map<int,string>::iterator ite1 = map1.begin(); ite1 != map1.end();ite1++)
 8     {
 9         cout<<ite1->first<<":"<<ite1->second<<endl;
10     }
11     cout<<endl;
12     
13 for(vector<int>::iterator ite1 = vec1.begin();ite1 < vec1.end();ite1++)
14     {
15         cout <<*ite1<<" ";
16     }
17     cout<<endl;

在使用迭代器访问数组元素的时候,可以看到,对于vevtor类型的iterator,重载了<,因此可以使用<作为循环终止条件,但对于set和map而言,并未重载<(其实仔细思考一下,由于set和map的实现结构是二叉树,其内存并不是连续的,因此并不能重载<),只能使用 !=。当然vector也可以使用!=。因此,为了不出错,最好都使用!=。

posted @ 2019-12-29 10:15  少年π  阅读(198)  评论(0编辑  收藏  举报