//tuple多元数组,必须是静态数组,类似结构体
//配合array,vector使用
//std::tuple<数组元素类型>数组变量名(数组元素变量名);
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每一个结点是映射链表的开头
1 map
2 multimap
1 map
映射(map) 由{键,值}对组成的集合,以某种作用于键对上的谓词排列 <map>
如果已经有重复的元素,将会插入失败
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 }
映射
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 }