复杂map初始化

复杂一点map初始化

1     //构造 map  复杂一点的 等等 key和value是任何值
2     map<string,vector<string > > mp;
3     map<int,map<string,vector<string> > > mps;

测试代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 void show(map<string,vector<string> >& mp)
 4 {
 5     map<string,vector<string> >::iterator iter=mp.begin();
 6     while(iter!=mp.end())
 7     {
 8         cout<<iter->first<<" :";
 9         for(int i=0;i<(int)iter->second.size();i++){
10             cout<<iter->second[i]<<" ,";
11         }
12         cout<<endl;
13         iter++;
14     }
15 }
16 int main()
17 {
18 
19     //构造 map  复杂一点的
20     map<string,vector<string > > mp;
21 
22     map<int,map<string,vector<string> > > mps;
23     mps[1]["ab"].push_back("ab ab....");
24     cout<<mps[1]["ab"][0]<<endl;
25 
26     //insert方法和 数组方法(直接赋值)比较
27 
28     //1:使用直接赋值 就算键已经存在,使用数组方法,将会直接更新键对应的值。
29     mp["abc"].push_back("string zero");
30     mp["abc"].push_back("string zero zero");
31 
32 
33     //2:使用insert    Insert方法不能覆盖,如果键已经存在,则插入失败
34     if(mp.find("abcd")==mp.end()){//不存在
35         vector<string> vs;
36         vs.push_back("string one");
37         vs.push_back("string one one");
38         mp.insert(make_pair("abcd",vs));//
39     }
40     else { //存在,就放进去
41         mp["abcd"].push_back("string two");
42         mp["abcd"].push_back("string two two");
43     }
44     show(mp);
45     return 0;
46 }
47 ab ab....
48 abc :string zero ,string zero zero ,
49 abcd :string one ,string one one ,

运行结果:

1 ab ab....
2 abc :string zero ,string zero zero ,
3 abcd :string one ,string one one ,

猜你喜欢

转载自www.cnblogs.com/NirobertEinteson/p/11967603.html