STL map+vector+struct的使用示例

时间:2022-12-15 20:48:41

个人编写的小例子,没有注释,刚毕业时作为技术调研随手编写,仅供参考:

 1 #include<iostream>
 2 #include<map>
 3 #include<vector>
 4 
 5 using namespace std;
 6 
 7 struct date_s
 8 {
 9         long id;
10         string obj_type;
11         string obj_name;
12 };
13 typedef vector<date_s> S_vector;
14 int main(int argc,char ** argv)
15 {
16         int table_no;
17         vector<date_s> v_data;
18         map<int,S_vector> m;
19         map<int,S_vector>::iterator it_m;
20 
21         date_s d1,d2,d3;
22         d1.id = 1;
23         d1.obj_name = "lijin";
24         d1.obj_type = "man";
25 
26         d2.id = 2;
27         d2.obj_name = "duanxiang";
28         d2.obj_type = "man";
29 
30         d3.id = 3;
31         d3.obj_name = "yuandong";
32         d3.obj_type = "man";
33 
34         v_data.push_back(d1);
35         v_data.push_back(d2);
36         v_data.push_back(d3);
37         m[1] = v_data;
38         S_vector tmp;
39 
40         it_m = m.begin();
41         tmp = it_m->second;
42 
43         cout<<it_m->second.front().id<<endl;
44 
45         for(;it_m != m.end();it_m++)
46         {
47         // cout<<it_m->first<<":"<<it_m->second.id<<" "<<it_m->second.obj_name<<endl;
48 
49         }
50 
51         return 0;
52 }