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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

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

1. Programming can be used to develop various software and applications, including websites, mobile applications, games, and data analysis tools. Its application fields are very wide, covering almost all industries, including scientific research, health care, finance, education, entertainment, etc. 2. Learning programming can help us improve our problem-solving skills and logical thinking skills. During programming, we need to analyze and understand problems, find solutions, and translate them into code. This way of thinking can cultivate our analytical and abstract abilities and improve our ability to solve practical problems.

Collection of C++ programming puzzles: stimulate thinking and improve programming skills Collection of C++ programming puzzles: stimulate thinking and improve programming skills Jun 01, 2024 pm 10:26 PM

C++ programming puzzles cover algorithm and data structure concepts such as Fibonacci sequence, factorial, Hamming distance, maximum and minimum values ​​of arrays, etc. By solving these puzzles, you can consolidate C++ knowledge and improve algorithm understanding and programming skills.

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

The difference between templates and generics in C++: Templates: defined at compile time, clearly typed, high efficiency, and small code size. Generics: runtime typing, abstract interface, provides flexibility, low efficiency.

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

When AI Ideograms compete for realism and artistic sense, Ideogram has opened up a tricky track: it can accurately generate text on pictures, and the fonts and layouts are beautiful. This demand is not niche. Generate posters and illustrations with one click without using P-pictures. It can save a lot of trouble and is very suitable for ordinary people who know nothing about design. We previously wrote about version 1.0 of Ideogram. On August 21st, version 2.0 came. The realism is better, the posters are more designed, and the special skill of text is also stronger. You may have never heard of it. This is an AI product developed by former Google employees. It has many shortcomings, but the longboard can "overtake" Midjourney in corners. Directions https://ideogram.ai/A

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

Pythonempowersbeginnersinproblem-solving.Itsuser-friendlysyntax,extensivelibrary,andfeaturessuchasvariables,conditionalstatements,andloopsenableefficientcodedevelopment.Frommanagingdatatocontrollingprogramflowandperformingrepetitivetasks,Pythonprovid

What are the common applications of C++ templates in actual development? What are the common applications of C++ templates in actual development? Jun 05, 2024 pm 05:09 PM

C++ templates are widely used in actual development, including container class templates, algorithm templates, generic function templates and metaprogramming templates. For example, a generic sorting algorithm can sort arrays of different types of data.

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

Python is an ideal programming introduction language for beginners through its ease of learning and powerful features. Its basics include: Variables: used to store data (numbers, strings, lists, etc.). Data type: Defines the type of data in the variable (integer, floating point, etc.). Operators: used for mathematical operations and comparisons. Control flow: Control the flow of code execution (conditional statements, loops).

Unleash Your Inner Programmer: C for Absolute Beginners Unleash Your Inner Programmer: C for Absolute Beginners Oct 11, 2024 pm 03:50 PM

C is an ideal language for beginners to learn programming, and its advantages include efficiency, versatility, and portability. Learning C language requires: Installing a C compiler (such as MinGW or Cygwin) Understanding variables, data types, conditional statements and loop statements Writing the first program containing the main function and printf() function Practicing through practical cases (such as calculating averages) C language knowledge

See all articles