在C语言中typedef将一个变量提升为一种类型:
- typedef int * p;//p是int*类型
-
- //int Array[10];//Array是一个可装10个int类型变量的数组。
- typedef int Array[10];//Array是一个可装10个int类型变量的数组的类型
- //Array arr;
- //sizeof(Array)//40
在C++中提供了更明确的类型重命名规则using
上面两种类型重命名可以改成:
- using p = int*;
- using Array = int[10];
using可以很方便的定义两种类型的集合,如下,将My_Pair的键值对两个类型定义成一个value_type。
- template<class _T1, class _T2>
- struct My_Pair
- {
- using first_type = _T1;
- using second_type = _T2;
- first_type first;
- second_type second;
- };
-
- template<class _Key, class _Ty>
- class My_Map
- {
- public:
- using value_type = My_Pair<_Key, _Ty>;
- value_type data;
- };
value_type是两种类型的集合,使用时给两个类型。
My_Mapint>simap;
map是C++ STL库中的一个关联式容器,其元素是一对键值对(key-value),可通过键查找值。其中,键和值可以是任意类型,但键必须是唯一的。map中的元素按照键的大小自动排序,因此可以快速地查找、插入和删除元素。map是一个非常常用的容器,常用于需要按照键进行查找的场合。
map的底层是红黑树, 红黑树具有良好的效率,它可在 O(logN) 时间内完成查找、增加、删除等操作。红黑树是一种接近平衡的二叉树。
map里面存的是pair,pair有first键和second值两个成员。
- map
int> simap; -
-
- /*这样写更方便*/
- using NAMap = map
int>; - NAMap sm; //创建容器
- sm.insert(NAMap::value_type("xiaoming", 20));//插入数据
方法一insert
容器名.insert(map<键类型,值类型>::value_type(键,值));
- // 类型加括号,调用构造函数
- simap.insert(map
int>::value_type("xiaoming", 20)); - //map不允许关键码重复
- simap.insert(map
int>::value_type("小红", 20)); - simap.insert(map
int>::value_type("小华", 20));
方法二operator[]
输入的数据放到红黑树中,若红黑树有name(name输入重复了),用age替换原有的;没有,建立结点key = name,val = age;
- string name;
- int age;
- while (cin >> name >> age, name != "end")
- {
- simap[name] = age;//重载[]直接赋值
- for (auto& x : simap)//输出数据
- {
- cout << x.first << " " << x.second << endl;
- }
- }
方法一:范围for
auto根据实际值推演出变量类型,const防止值被修改,&引用赋值不构建将亡值更快。
- for (const auto& x : simap)//范围for
- {
- cout << x.first << " " << x.second << endl;
- }
方法二:迭代器iterator输出
创建map迭代器指向map中第一个值
mapint>::iterator it = simap.begin();
也可以用auto自动识别map的类型创建迭代器auto it = simap.begin();//这样写更方便
- for (; it != simap.end(); it++)
- {
- cout << it->first << " " << it->second << endl;
- }
- string name;
- while (cin >> name, name != "end")
- {
- try
- {
- auto x = simap.at(name);//at()相当于数组下标,但是比[]更安全
- cout << typeid(x).name() << endl;//运行时类型识别RTTI,输出类型
- cout << x << endl;//输出值
- }
- catch (std::out_of_range& e)//捕获超出范围的异常
- {
- cout << e.what() << endl;
- }
- }
