专业做卖菜的网站,上海建站推广公司,wordpress 图书模板,网站开发哪个更专业using语法和typedef一样#xff0c;并不会创建出新的类型#xff0c;它们只是给某些类型定义了新的别名。using相较于typedef的优势在于定义函数指针别名时看起来更加直观#xff0c;并且可以给模板定义别名。
使用typedef给模板定义别名:
无法直接使用typedef给模板定义别…using语法和typedef一样并不会创建出新的类型它们只是给某些类型定义了新的别名。using相较于typedef的优势在于定义函数指针别名时看起来更加直观并且可以给模板定义别名。
使用typedef给模板定义别名:
无法直接使用typedef给模板定义别名
代码如下:
templatetypename T
typedef mapint, TmapType;//error注意:使用typedef给模板定义别名,需要用到struct
代码如下:
#include iostream
#include string
#include map
using namespace std;templatetypename T
struct myMap
{typedef mapint, TmapType;
};templatetypename T
class Container
{
public:void print(T t){auto it t.begin();for (; it ! t.end(); it){cout it-first it-second endl;}}
};int main()
{myMapint::mapType mm1;mm1.insert(make_pair(1, 1));mm1.insert(make_pair(2, 2));mm1.insert(make_pair(3, 3));ContainermyMapint::mapType q;q.print(mm1);myMapdouble::mapType mm2;mm2.insert(make_pair(1, 1.1));mm2.insert(make_pair(2, 2.2));mm2.insert(make_pair(3, 3.3));ContainermyMapdouble::mapType q1;q1.print(mm2);myMapstring::mapType mm3;mm3.insert(make_pair(3, Tom));mm3.insert(make_pair(2, Jack));mm3.insert(make_pair(1, Hello));ContainermyMapstring::mapType q2;q2.print(mm3);return 0;
}测试结果: 使用using给模板定义别名:
代码如下:
#include string
#include iostream
#include map
using namespace std;templatetypename T
class Container
{
public:void print(T t){auto it t.begin();for (; it ! t.end(); it){cout it-first it-second endl;}}
};templatetypename T
using myMap mapint, T;int main()
{myMapstring mm3;mm3.insert(make_pair(1, Tom));mm3.insert(make_pair(2, jack));mm3.insert(make_pair(3, hello));ContainermyMapstring t;t.print(mm3);return 0;
}测试结果: