#include

 

//tuple多元數組,必須是靜態數組,相似結構體ios

//配合array,vector使用數組

//std::tuple<數組元素類型>數組變量名(數組元素變量名);ui

 

 1 #include <iostream>
 2 #include <map>
 3 using namespace std;
 4 
 5 //tuple多元數組,必須是靜態數組,相似結構體
 6 //配合array,vector使用
 7 
 8 void main()
 9 {
10     int int1(10);
11     double double1(99.8);
12     char ch1('A');
13     char *str = "hello";
14 
15     //std::tuple<數組元素類型>數組變量名(數組元素變量名);
16     std::tuple<int, double, char, const char *>mytuple(int1, double1, ch1, str);//建立一個多元數組,能夠存放不一樣類型的元素
17 
18     auto data0 = std::get<0>(mytuple);//下標只能是常量
19     auto data1 = std::get<1>(mytuple);
20     auto data2 = std::get<2>(mytuple);
21     auto data3 = std::get<3>(mytuple);
22 
23     std::cout << typeid(data0).name() << " " << data0 << std::endl;//在C++中,typeid用於返回指針或引用所指對象的實際類型。
24     std::cout << typeid(data1).name() << " " << data1 << std::endl;
25     std::cout << typeid(data2).name() << " " << data2 << std::endl;
26     std::cout << typeid(data3).name() << " " << data3 << std::endl;
27 
28     decltype(data0) dataA;//根據一個變量,建立一個備份
29 
30     system("pause");
31 }

 

map, multimap的區別:map每個結點是映射,multimap每個結點是映射鏈表的開頭spa

 

1 map指針

2 multimapcode

 

1 map對象

映射(map) 由{鍵,值}對組成的集合,以某種做用於鍵對上的謂詞排列 <map>blog

 

若是已經有重複的元素,將會插入失敗get

 

 1 #include <iostream>
 2 #include <map>
 3 
 4 int main()
 5 {
 6     std::map<const char *, int>mymap;
 7 
 8     mymap.insert(std::pair<const char *, int>("司令1", 101));
 9     mymap.insert(std::pair<const char *, int>("司令2", 102));
10     mymap.insert(std::pair<const char *, int>("司令3", 103));
11     mymap.insert(std::pair<const char *, int>("司令4", 104));
12 
13     mymap.insert(std::pair<const char *, int>("司令1", 105));//若是已經有重複的元素,將會插入失敗
14 
15     auto ib = mymap.begin();
16     auto ie = mymap.end();
17 
18     for (; ib != ie; ib++)
19     {
20         std::cout << ib->first << " " << ib->second << std::endl;
21     }
22     
23     return 0;
24 }

 

映射string

 

 1 #include <iostream>
 2 #include <map>
 3 
 4 struct student
 5 {
 6     char *name;
 7     int year;
 8 };
 9 
10 struct stuinfo
11 {
12     int id;
13     student stu;
14 };
15 
16 int main()
17 {
18     stuinfo infoarray[] = { {10,{"hello",21}},{ 5,{ "hello",20 }},{25,{ "hello",30 }} };
19 
20     std::map<int, student>m;
21 
22     for (int i = 0; i < 3; i++)
23     {
24         m[infoarray[i].id] = infoarray[i].stu;
25     }
26 
27     auto ib = m.begin();
28     auto ie = m.end();
29 
30     for (; ib != ie; ib++)//遍歷
31     {
32         std::cout << (*ib).first << " ";
33         std::cout << (*ib).second.name << " " << (*ib).second.year << std::endl;
34     }
35 
36     return 0;
37 }

 

2 multimap

 

 1 #include <iostream>
 2 #include <map>
 3 
 4 int main()
 5 {
 6     std::multimap<const char *, int>mymap;
 7 
 8     mymap.insert(std::pair<const char *, int>("司令1", 101));
 9     mymap.insert(std::pair<const char *, int>("司令2", 102));
10     mymap.insert(std::pair<const char *, int>("司令3", 103));
11     mymap.insert(std::pair<const char *, int>("司令4", 104));
12 
13     mymap.insert(std::pair<const char *, int>("司令1", 105));//multimap若是有重複,也會插入成功
14 
15     auto ib = mymap.begin();
16     auto ie = mymap.end();
17 
18     for (; ib != ie; ib++)
19     {
20         std::cout << ib->first << " " << ib->second << std::endl;
21     }
22     
23     return 0;
24 }

 

multimap的元素能夠重複,所以equal_range(const T&);能夠實現查找多個相同的元素

 

 1 #include <iostream>
 2 #include <map>
 3 #include <string>
 4 
 5 void main()
 6 {
 7     std::multimap<std::string, std::string>mymap;
 8 
 9     mymap.insert(std::pair<std::string, std::string>("hello", "a"));
10     mymap.insert(std::pair<std::string, std::string>("world", "b"));
11     mymap.insert(std::pair<std::string, std::string>("hello", "c"));
12     mymap.insert(std::pair<std::string, std::string>("world", "d"));
13 
14     auto ib = mymap.begin();
15     auto ie = mymap.end();
16 
17     for (; ib != ie; ib++)
18     {
19         std::cout << ib->first << " " << ib->second << std::endl;
20     }
21     std::cout << std::endl;
22 
23     auto pfind = mymap.find("hello");//查找
24     std::cout << pfind->first << " " << pfind->second << std::endl << std::endl;//只輸出查找的第一個元素
25 
26     auto it = mymap.equal_range("hello");
27     for (auto i = it.first; i != it.second; i++)
28     {
29         std::cout << i->first << " " << i->second << std::endl;
30     }
31 }
相關文章
相關標籤/搜索