杨凌网站建设公司,做网站推广哪些,阿里巴巴黄页网1688,网站开发目的和意义众所周知#xff0c;在 C 中可以使用 typedef 定义类型别名#xff0c;例如#xff1a;
typedef unsigned int u_int;
typedef void(*pf)(int, int);
但它也有一些限制#xff0c;比如#xff0c;无法定义类模板别名。当我们需要实现一个 key_type 固定为 string#x…众所周知在 C 中可以使用 typedef 定义类型别名例如
typedef unsigned int u_int;
typedef void(*pf)(int, int);
但它也有一些限制比如无法定义类模板别名。当我们需要实现一个 key_type 固定为 stringmapped_type 可能为 string、int 等的 map_str 类模板时直接使用 typedef 是行不通的往往不得不按以下方式去写
#include map
#include string
#include iostream
using namespace std;
templateclass T
struct map_str
{typedef mapstring, T type;
};
int main()
{map_strstring::type dictMap;dictMap.insert({ hello, 你好 });dictMap.insert({ world, 世界 });for (const auto kv : dictMap){cout kv.first : kv.second endl;}
string fruits[] { 苹果, 西瓜, 苹果, 西瓜, 苹果, 苹果, 西瓜,苹果, 香蕉, 苹果, 香蕉 };map_strint::type countMap;for (const auto e : fruits){countMap[e];}for (const auto kv : countMap){cout kv.first : kv.second endl;}return 0;
}
在 C11 中不仅可以使用 using 定义类型别名还可以定义类模板别名。
using u_int unsigned int;
using pf void(*)(int, int);
// 与 typedef 相比using 后面总是立即出现类型别名
// 之后使用类似赋值语法将现有的类型名赋给新的类型别名
templateclass T
using map_str mapstring, T;
int main()
{map_strstring dictMap;map_strint countMap;return 0;
}