Home Database Mysql Tutorial 模板元编程简介2

模板元编程简介2

Jun 07, 2016 pm 03:19 PM
number power use Can template Solve Introduction programming this

2:求解一个数的乘方。 当然这个可以利用cmath头文件中pow函数来完成,但对于次数较小的常整数的乘方运算来说,这种办法的效率较低,不如手工写一个操作数连乘的表达式,但有时候这样并不方便,特别当乘方运算的底数本身是一个较为复杂的表达式时,一般还要

2:求解一个数的乘方。

当然这个可以利用cmath头文件中pow函数来完成,但对于次数较小的常整数的乘方运算来说,这种办法的效率较低,不如手工写一个操作数连乘的表达式,但有时候这样并不方便,特别当乘方运算的底数本身是一个较为复杂的表达式时,一般还要先用临时变量将表达式保存,再对临时变量做乘方。通过定义一个如下的内联函数可以提供一些方便。


inline double power(double x, unsigned n)

{

double result = x;

for(int i = 1; i

result *= x;

return result;

}

当n比较小时,这个函数的效率通常会比cmath头文件的pow函数高,但这要在运行时执行循环,并没有达到理想的效率,模板元又派上用场了。如下:

template

inline double power(double v)

{

return v * power(v);

}

template

inline double power(double v)

{

return v;

}


上面的模板不够通用,只能针对double类型。下面引用新的类型参数T,由于函数模板不支持偏特化,我们不便直接指定N=1时的结果,因此可以借助于一个类模板。

template

struct Power

{

template

static T value(T x)

{

return x * Power::value(x);

};

template

struct Power

{

template

static T value

{

reurn x;

}


这样,我们求x的4次方,可以这样写:Power::value(x);

但是这样写很不方便,所以我们可以写一个辅助的模板函数,如下:

template

{

inline T Power(T v)

return Power::value(v);

}

这样,x的4次方就可以这样来写:power(x);

}

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is programming for and what is the use of learning it? What is programming for and what is the use of learning it? Apr 28, 2024 pm 01:34 PM

What is programming for and what is the use of learning it?

Comparison of C++ templates and generics? Comparison of C++ templates and generics? Jun 04, 2024 pm 04:24 PM

Comparison of C++ templates and generics?

Limitations of C++ templates and how to circumvent them? Limitations of C++ templates and how to circumvent them? Jun 02, 2024 pm 08:09 PM

Limitations of C++ templates and how to circumvent them?

The Key to Coding: Unlocking the Power of Python for Beginners The Key to Coding: Unlocking the Power of Python for Beginners Oct 11, 2024 pm 12:17 PM

The Key to Coding: Unlocking the Power of Python for Beginners

Magically modified 'Black Myth: Wukong ' to defeat Midjourney. This AI drawing tool is amazing. Magically modified 'Black Myth: Wukong ' to defeat Midjourney. This AI drawing tool is amazing. Aug 23, 2024 pm 09:42 PM

Magically modified 'Black Myth: Wukong ' to defeat Midjourney. This AI drawing tool is amazing.

Java Made Simple: A Beginner's Guide to Programming Power Java Made Simple: A Beginner's Guide to Programming Power Oct 11, 2024 pm 06:30 PM

Java Made Simple: A Beginner's Guide to Programming Power

Create the Future: Java Programming for Absolute Beginners Create the Future: Java Programming for Absolute Beginners Oct 13, 2024 pm 01:32 PM

Create the Future: Java Programming for Absolute Beginners

Problem-Solving with Python: Unlock Powerful Solutions as a Beginner Coder Problem-Solving with Python: Unlock Powerful Solutions as a Beginner Coder Oct 11, 2024 pm 08:58 PM

Problem-Solving with Python: Unlock Powerful Solutions as a Beginner Coder

See all articles