当前位置:网站首页>函数模板学习记录
函数模板学习记录
2022-07-25 06:01:00 【熊猫小将】
概念
- 函数模板,用于适配多种参数类型,可以避免重复的代码。
1.模板函数调用方式
template <typename T >
T Add(T t1, T t2)
{
return t1 + t2;
}
template <typename T1, typename T2 >
T1 Sub(T1 t1, T2 t2)
{
return t1 + t2;
}
int main()
{
int a = 2;
int b = 4;
double a1 = 1.3;
double a2 = 2.3;
Add(a,b); //① 调用函数模板;
Add<>(a,b); //与①效果一样,调用函数模板,此处<>无实际作用;
Add(a, a1); //此时编译器会报错,因为两个参数类型不一致;
Add<double>(a, a1); //此时<>生效,将执行的结果转为double类型
Sub(a,a1);
std::cout << Add<double>(a, a1) << std::endl;
std::cout << Add<>(a, b) << std::endl;
std::cin.get();
}
2.当普通函数和模板函数同时存在
void func()
{
std::cout << "普通函数" << std::endl;
}
template <typename T>
void func()
{
std::cout << "模板函数" << std::endl;
}
int main()
{
int a = 2;
int b = 4;
double a1 = 1.3;
double a2 = 2.3;
func(); //调用的是普通函数,如果将上面普通函数注释掉编译器则则会报错
func<int>(); //调用的是模板函数,需要显示的传入推导参数类型,否则会报错
std::cin.get();
}
3.模板参数默认值
//默认参数类型
template <typename T1, typename T2 = int>
void funcT1(T1 a, T2 b = 123)
{
std::cout << "a:"<<a << std::endl;
std::cout << "b:" << b << std::endl;
}
template <typename T1 = int, typename T2>
void funcT2(T2 b)
{
T1 a = b;
std::cout << "b:" << b << std::endl;
std::cout << "a:" << a << std::endl;
}
template <typename T1, int T2 = 123>
void funcT3(T1 a)
{
std::cout << "a:" << a << std::endl;
std::cout << "T2:" << T2 << std::endl;
}
int main()
{
int a = 2;
int b = 4;
double a1 = 1.3;
double a2 = 2.3;
funcT1(a,a1); //参数2默认为整型
funcT2(a1); //将浮点型强转为整型
funcT2<double>(a1); //参数1显示声明为浮点型,这样就无需类型转换了
funcT3(5);
funcT3<int,245>(5); //参数2必须为常量,在编译期间就能获取当前的值
std::cin.get();
}
4.反编译指令
即将编译生成的a文件反编译为二进制文件binary.txt
- Linux环境命令
objdump -DC a.out>binary.txt
- Win环境命令
win dumpbin /all a.obj>binary.txt
边栏推荐
- Sword finger offer 54. the k-th node of the binary search tree
- Programming hodgepodge (I)
- New discovery of ROS callback function
- ECS is exclusive to old users, and the new purchase of the remaining 10 instances is as low as 3.6% off
- 阻塞队列分析
- Leetcode 237. delete nodes in the linked list
- [node] the service port is occupied error: listen eaddinuse: address already in use::: 9000- how to close the port started by node
- R language obtains the data row where the nth maximum value of the specified data column is located in the data.table data
- A little experience about von Mises distribution
- Why is it that all the games are pseudorandom and can't make true random?
猜你喜欢

HTB-Arctic

QT qtextedit setting qscrollbar style sheet does not take effect solution

Sword finger offer 54. the k-th node of the binary search tree

Unity animator animation and state machine

Blocking Queue Analysis

Ffmpeg notes (I) fundamentals of audio and video

Softing pngate series gateway: integrate PROFIBUS bus into PROFINET network

VO, dto, do, Po distinction and use

HTB-Optimum

Linear algebra (3)
随机推荐
剑指 Offer 45. 把数组排成最小的数
Leetcode/ number of 1 in the first n digit binary
What are the ways to realize web digital visualization?
[QT] solve the problem of Chinese garbled code output from QT console
(Niuke multi School II) G-LINK with monotonic subsequence (construction question)
Common methods of JS operation array
For data security reasons, the Dutch Ministry of Education asked schools to suspend the use of Chrome browser
Promise implementation
(2022牛客多校二)K-Link with Bracket Sequence I(动态规划)
R language data The table package performs aggregation transforms of data packets and calculates the grouping interquartile range (IQR) of dataframe data
ABC 261.D - Flipping and Bonus ( DP )
A little experience about von Mises distribution
Dynamic planning learning notes
Msys2 common configuration
Switch NPM source to private source library
Unity accesses chartandgraph chart plug-in
ROI pooling and ROI align
Sword finger offer 32 - I. print binary tree from top to bottom
2021年ICPC陕西省赛热身赛 B.CODE(位运算)
对于von Mises distribution(冯·米塞斯分布)的一点心得