C++ 模板练习

时间:2019-09-28 19:10 作者:DoubleQ 阅读数:533

// testTemplate.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。

//


#include <iostream>

using namespace std;

template <typename T>

T getAbs(T t)

{

if (t >= 0)

{

return t;

}

else

{

return -t;

}

}

template <typename T>

T getMax(T* t, int n)

{

T v;

for (int i = 0; i < n; i++)

{

if (i == 0)

{

v = t[i];

}

else

{

if (t[i] > v)

{

v = t[i];

}

}

}

return v;

}

template <typename T1, typename T2, typename T3, typename T4, typename T5>

void show(T1 t1,T2 t2,T3 t3,T4 t4,T5 t5)

{

std::cout << "\n";

std::cout << t1 << "\n";

std::cout << t2 << "\n";

std::cout << t3 << "\n";

std::cout << t4 << "\n";

std::cout << t5 << "\n";


}

template<typename T>

struct Data

{

string key;

T value;

};

int main()

{

    std::cout <<getAbs<int>(-3)<<"\n";

std::cout << getAbs<int>(1) << "\n";

std::cout << getAbs<int>(-1.1) << "\n";

std::cout << getAbs<int>(0) << "\n";

std::cout << getAbs<int>(22.1) << "\n";

int a[] = {1,5,3,2,199,-3};

std::cout << getMax<int>(a, 6) << "\n";


show<int, double, string, int, int>(1,101,"aa",3,88);


Data<int> d1{ "aa",100 };

Data<string> d2{ "bb","ssss" };

cout << d1.key << "\n";

cout << d1.value << "\n";

cout << d2.key << "\n";

cout << d2.value << "\n";

}


image.png


推荐文章

[推荐] .NET Core 学习资料精选:入门

[推荐] 数据库常见死锁原因及处理

[推荐] C# 设置Excel数据自适应行高、列宽的2种情况 (转)

[推荐] C++ vector 和set结合数字去重练习

[推荐] win7 .net core 部署 The library hostfxr.dll was found, but loading it from C:\<path_to_app>\hostfxr.dll failed

[推荐] JdbcTemplate多数据源使用

[推荐] 深入学习Redis(2):持久化

[推荐] asp.net core系列 67 Web压力测试工具WCAT

[推荐] .Net Core 2.0 获取客户端IP地址

[推荐] 第七天 让你的container实现跨主机访问

[推荐] nodejs app.use(express.static) 静态文件托管 (四)

[推荐] 8天玩转并行开发——第八天 用VS性能向导解剖你的程序

[推荐] C++ Lambda表达式练习

[推荐] C++ 稀疏表的练习

[推荐] Spring Boot 2 (五):Docker Compose + Spring Boot + Nginx + Mysql 实践

[推荐] asp.net core系列 33 EF查询数据 (2)

[推荐] 试试发布

[推荐] C++中 *p++ 到底是 *(p++) 还是 (*p)++ 1. 猜想

[推荐] JavaWeb-Log4j: WARN No appenders could be found for logger

[推荐] C++ 模板练习


评论列表



注册用户登录后才能发表评论,请登录注册,访问 博客首页