//tuple多元数组,必须是静态数组,类似结构体

//配合array,vector使用

//std::tuple<数组元素类型>数组变量名(数组元素变量名);

 #include <iostream>
#include <map>
using namespace std; //tuple多元数组,必须是静态数组,类似结构体
//配合array,vector使用 void main()
{
int int1();
double double1(99.8);
char ch1('A');
char *str = "hello"; //std::tuple<数组元素类型>数组变量名(数组元素变量名);
std::tuple<int, double, char, const char *>mytuple(int1, double1, ch1, str);//创建一个多元数组,可以存放不同类型的元素 auto data0 = std::get<>(mytuple);//下标只能是常量
auto data1 = std::get<>(mytuple);
auto data2 = std::get<>(mytuple);
auto data3 = std::get<>(mytuple); std::cout << typeid(data0).name() << " " << data0 << std::endl;//在C++中,typeid用于返回指针或引用所指对象的实际类型。
std::cout << typeid(data1).name() << " " << data1 << std::endl;
std::cout << typeid(data2).name() << " " << data2 << std::endl;
std::cout << typeid(data3).name() << " " << data3 << std::endl; decltype(data0) dataA;//根据一个变量,创建一个备份 system("pause");
}

map, multimap的区别:map每一个结点是映射,multimap每一个结点是映射链表的开头

1 map

2 multimap

1 map

映射(map) 由{键,值}对组成的集合,以某种作用于键对上的谓词排列 <map>

如果已经有重复的元素,将会插入失败

 #include <iostream>
#include <map> int main()
{
std::map<const char *, int>mymap; mymap.insert(std::pair<const char *, int>("司令1", ));
mymap.insert(std::pair<const char *, int>("司令2", ));
mymap.insert(std::pair<const char *, int>("司令3", ));
mymap.insert(std::pair<const char *, int>("司令4", )); mymap.insert(std::pair<const char *, int>("司令1", ));//如果已经有重复的元素,将会插入失败 auto ib = mymap.begin();
auto ie = mymap.end(); for (; ib != ie; ib++)
{
std::cout << ib->first << " " << ib->second << std::endl;
} return ;
}

映射

 #include <iostream>
#include <map> struct student
{
char *name;
int year;
}; struct stuinfo
{
int id;
student stu;
}; int main()
{
stuinfo infoarray[] = { {,{"hello",}},{ ,{ "hello", }},{,{ "hello", }} }; std::map<int, student>m; for (int i = ; i < ; i++)
{
m[infoarray[i].id] = infoarray[i].stu;
} auto ib = m.begin();
auto ie = m.end(); for (; ib != ie; ib++)//遍历
{
std::cout << (*ib).first << " ";
std::cout << (*ib).second.name << " " << (*ib).second.year << std::endl;
} return ;
}

2 multimap

 #include <iostream>
#include <map> int main()
{
std::multimap<const char *, int>mymap; mymap.insert(std::pair<const char *, int>("司令1", ));
mymap.insert(std::pair<const char *, int>("司令2", ));
mymap.insert(std::pair<const char *, int>("司令3", ));
mymap.insert(std::pair<const char *, int>("司令4", )); mymap.insert(std::pair<const char *, int>("司令1", ));//multimap如果有重复,也会插入成功 auto ib = mymap.begin();
auto ie = mymap.end(); for (; ib != ie; ib++)
{
std::cout << ib->first << " " << ib->second << std::endl;
} return ;
}

multimap的元素可以重复,因此equal_range(const T&);可以实现查找多个相同的元素

 #include <iostream>
#include <map>
#include <string> void main()
{
std::multimap<std::string, std::string>mymap; mymap.insert(std::pair<std::string, std::string>("hello", "a"));
mymap.insert(std::pair<std::string, std::string>("world", "b"));
mymap.insert(std::pair<std::string, std::string>("hello", "c"));
mymap.insert(std::pair<std::string, std::string>("world", "d")); auto ib = mymap.begin();
auto ie = mymap.end(); for (; ib != ie; ib++)
{
std::cout << ib->first << " " << ib->second << std::endl;
}
std::cout << std::endl; auto pfind = mymap.find("hello");//查找
std::cout << pfind->first << " " << pfind->second << std::endl << std::endl;//只输出查找的第一个元素 auto it = mymap.equal_range("hello");
for (auto i = it.first; i != it.second; i++)
{
std::cout << i->first << " " << i->second << std::endl;
}
}
04-15 01:14