Skip to content

Commit 67aaa8f

Browse files
authored
Create map.cpp
1 parent f3e9914 commit 67aaa8f

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

STL/map.cpp

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#include<iostream>
2+
#include<map>
3+
#include<set>
4+
using namespace std;
5+
6+
void display(const std::map<std::string,std::set<int>> &m)
7+
{
8+
cout<<"[ ";
9+
for(const auto &t:m)
10+
{
11+
cout<<t.first<<":[";
12+
for(const auto & s:t.second)
13+
std::cout<<s<<" ";
14+
std::cout<<"]";
15+
}
16+
cout<<"]"<<endl;
17+
}
18+
19+
template <typename T1,typename T2>
20+
void display(const std::map<T1,T2> &l)
21+
{
22+
cout<<"[ ";
23+
for(const auto &t:l)
24+
{
25+
cout<<t.first<<":"<<t.second<<" ";
26+
}
27+
cout<<" ]"<<endl;
28+
}
29+
30+
void test1()
31+
{
32+
cout<<"test1=========================="<<endl;
33+
std::map<std::string,int>m{
34+
{"Larry",3},{"Moe",1},{"Curly",2}};
35+
display(m);
36+
m.insert(std::make_pair("Anna",10));
37+
display(m);
38+
m["Joe"]=5;
39+
display(m);
40+
m["Frank"]=18;
41+
display(m);
42+
m["Frank"]+=10;
43+
display(m);
44+
m.erase("Frank");
45+
display(m);
46+
cout<<"Count for Frank:"<<m.count("Frank")<<endl;
47+
cout<<"Count for Joe:"<<m.count("Joe")<<endl;
48+
if(m.find("Larry")!=m.end())
49+
cout<<"Found Larry"<<endl;
50+
auto it=m.find("Larry");
51+
if(it!=m.end())
52+
cout<<it->first<<":"<<it->second<<endl;
53+
m.clear();
54+
display(m);
55+
cout<<endl;
56+
}
57+
58+
void test2()
59+
{
60+
cout<<"test2=========================="<<endl;
61+
std::map<std::string,std::set<int>>m{
62+
{"Larry",{100,90}},{"Moe",{94}},{"Curly",{80,90,100}}};
63+
display(m);
64+
m["Larry"].insert(95);
65+
display(m);
66+
auto it=m.find("Moe");
67+
if(it!=m.end())
68+
it->second.insert(1000);
69+
display(m);
70+
cout<<endl;
71+
}
72+
73+
int main()
74+
{
75+
test1();
76+
test2();
77+
}

0 commit comments

Comments
 (0)