📄 power.html
字号:
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Author" CONTENT="Zafir Anjum">
<TITLE>MFC Programmer's SourceBook : STL Programmer's Guide</TITLE>
<META name="description"
content="A freely available implementation
of the C++ Standard Template Library, including
hypertext documentation.">
<META name="keywords"
content="generic programming, STL, standard template library">
</HEAD>
<SCRIPT LANGUAGE="JavaScript"><!--
var adcategory = "cpp";
// -->
</SCRIPT>
<body background="../../fancyhome/back.gif" bgcolor="#FFFFFF" >
<SCRIPT LANGUAGE="JavaScript"><!--
var nfrm = location.href.indexOf("_nfrm_");
var validframes = (top.frames.length > 0 && top.frames['ad'] && top.frames['logo'] );
var random = Math.random();
if( !validframes && nfrm == -1 )
{
var dclkPage = "www.codeguru.com/";
if( self.adcategory )
dclkPage += adcategory;
else
dclkPage += "mfc";
document.write('<nolayer><center>');
document.write('<iframe src="http://ad.doubleclick.net/adi/' + dclkPage + ';ord='
+ random + '" width=470 height=62 marginwidth=0 marginheight=0 hspace=0 vspace=0 '
+ 'frameborder=0 scrolling=no bordercolor="#000000">');
document.write('<a href="http://ad.doubleclick.net/jump/' + dclkPage + ';ord='
+ random + '">');
document.write('<img src="http://ad.doubleclick.net/ad/' + dclkPage + ';ord='
+ random + '" height=60 width=468>' + '</a>');
document.write('</iframe>');
document.write('</center></nolayer>');
document.write('<layer src="http://ad.doubleclick.net/adl/' + dclkPage +
';ord=' + random + '"></layer>');
document.write('<ilayer visibility=hide width=468 height=83></ilayer>');
}
// top.location = "/show.cgi?" + adcategory + "=" + location.pathname;
// -->
</SCRIPT>
<noscript>
<p align="center">
<a href="http://ad.doubleclick.net/jump/www.codeguru.com/cpp;ord=NupbdtFCY34AAHpzggo">
<img src="http://ad.doubleclick.net/ad/www.codeguru.com/cpp;ord=NupbdtFCY34AAHpzggo"></a>
</p>
</noscript>
<BR Clear>
<H1>power</H1>
<Table CellPadding=0 CellSpacing=0 width=100%>
<TR>
<TD Align=left><Img src = "algorithms.gif" Alt="" WIDTH = "194" HEIGHT = "38" ></TD>
<TD Align=right><Img src = "function.gif" Alt="" WIDTH = "194" HEIGHT = "38" ></TD>
</TR>
<TR>
<TD Align=left VAlign=top><b>Category</b>: algorithms</TD>
<TD Align=right VAlign=top><b>Component type</b>: function</TD>
</TR>
</Table>
<h3>Prototype</h3>
<tt>Power</tt> is an overloaded name; there are actually two <tt>power</tt>
functions.
<pre>
template <class T, class Integer>
inline T power(T x, Integer n);
template <class T, class Integer, class <A href="MonoidOperation.html">MonoidOperation</A>>
T power(T x, Integer n, MonoidOperation op);
</pre>
<h3>Description</h3>
<tt>Power</tt> is generalized exponentiation: it raises the value <tt>x</tt>
to the power <tt>n</tt>, where <tt>n</tt> is a non-negative integer.
<P>
The first version of <tt>power</tt>
returns <tt>x</tt> raised to the <tt>n</tt>th power; that is, it returns
<tt>x * x ... * x</tt>, where <tt>x</tt> is repeated <tt>n</tt> times. <A href="#1">[1]</A> If
<tt>n == 0</tt>, then it returns <tt><A href="MonoidOperation.html">identity_element</A>(<A href="times.html" tppabs="http://www.sgi.com/Technology/STL/times.shtml">multiplies</A><T>())</tt>.
<P>
The second version of <tt>power</tt> is just like the first except
that it uses an arbitrary <A href="MonoidOperation.html">Monoid Operation</A> instead of
<tt><A href="times.html">multiplies</A><T></tt>, returning <tt><A href="MonoidOperation.html" tppabs="http://www.sgi.com/Technology/STL/MonoidOperation.shtml">identity_element</A>(op)</tt>
when <tt>n == 0</tt>.
<P>
<b>Important note</b>: <tt>power</tt> does not assume that multiplication is
commutative, but it does rely crucially on the fact that
multiplication is associative. If you have defined <tt>*</tt> or
<tt><A href="MonoidOperation.html">MonoidOperation</A></tt> to be a non-associative operation, then <tt>power</tt>
<i>will give you the wrong answer</i>. <A href="#2">[2]</A>
<h3>Definition</h3>
Defined in <A href="algo.h">algo.h</A>.
<h3>Requirements on types</h3>
For the first version:
<UL>
<LI>
<tt><A href="times.html">multiplies</A><T></tt> is a model of <A href="MonoidOperation.html" tppabs="http://www.sgi.com/Technology/STL/MonoidOperation.shtml">Monoid Operation</A>.
<LI>
<tt>Integer</tt> is an integral type.
</UL>
For the second version:
<UL>
<LI>
<tt>MonoidOperation</tt> is a model of <A href="MonoidOperation.html">Monoid Operation</A>.
<LI>
<tt>T</tt> is <tt>MonoidOperation</tt>'s argument type.
<LI>
<tt>n</tt> is an integral type.
</UL>
<h3>Preconditions</h3>
<UL>
<LI>
<tt>n >= 0</tt>.
</UL>
<h3>Complexity</h3>
The number of multiplications (or, in the case of the second version,
the number of applications of <tt>MonoidOperation</tt>) is <tt>lg(n) + nu(n)</tt>
where <tt>lg</tt> is the base 2 logarithm and <tt>nu(n)</tt> is the number of
<tt>1</tt>s in the binary representation of <tt>n</tt>. <A href="#3">[3]</A>
<h3>Example</h3>
<pre>
int main() {
cout << "2 ** 30 = " << power(2, 30) << endl;
}
</pre>
<h3>Notes</h3>
<P><A name="1">[1]</A>
This is a conceptual description of what <tt>power</tt>'s return value
is; it is not how <tt>power</tt> is actually implemented. If <tt>power</tt>
were implemented that way then it would require <tt>n-1</tt> multiplications,
which would be grossly inefficient. <tt>Power</tt> is implemented using
the "Russian peasant algorithm", which requires only <tt>O(log n)</tt>
multiplications.
See section 4.6.3 of Knuth (D. E. Knuth, <i>The Art of Computer
Programming. Volume 2: Seminumerical Algorithms</i>,
Addison-Wesley, 1981) for a discussion.
<P><A name="2">[2]</A>
See the <A href="MonoidOperation.html">Monoid Operation</A> requirements for a discussion of associativity.
<P><A name="3">[3]</A>
This is in fact not the minimum possible number of multiplications:
it is possible to compute the fifteenth power of <tt>x</tt> using only
five multiplications, but <tt>power(x, 15)</tt> uses six.
<h3>See also</h3>
<A href="MonoidOperation.html">Monoid Operation</A>,
<tt><A href="times.html">multiplies</A></tt>,
<tt><A href="plus.html">plus</A></tt>
<HR SIZE="6"> <FONT SIZE="-2"> Copyright © 1996 Silicon Graphics, Inc.
<HR>
<TABLE BORDER=0 WIDTH="100%" >
<TR>
<TD WIDTH="33%"><FONT SIZE=-1><A HREF="index.html" >
STL</A></FONT></TD>
<TD WIDTH="33%">
<CENTER><FONT SIZE=-2>© Copyright 1997-1998 CodeGuru</FONT> </CENTER>
</TD>
<TD WIDTH="34%">
<DIV ALIGN=right><FONT SIZE=-1>Contact : <A HREF="mailto:webmaster@codeguru.com">webmaster@codeguru.com</A> </FONT></DIV>
</TD>
</TR>
</TABLE>
<SCRIPT LANGUAGE="JavaScript" ><!--
var adurl = "/cgi-bin/doubleclick.cgi?";
if( self.adcategory )
adurl += adcategory;
else
adurl += "mfc";
if( self.parent.norefreshad )
parent.norefreshad = false;
else if( validframes )
parent.frames['ad'].location = adurl;
if( !validframes && nfrm == -1)
{
var dclkPage = "www.codeguru.com/";
if( self.adcategory )
dclkPage += adcategory;
else
dclkPage += "mfc";
// var random = Math.random();
document.write('<nolayer><center>');
document.write('<iframe src="http://ad.doubleclick.net/adi/' + dclkPage + ';ord='
+ random + '" width=470 height=62 marginwidth=0 marginheight=0 hspace=0 vspace=0 '
+ 'frameborder=0 scrolling=no bordercolor="#000000">');
document.write('<a href="http://ad.doubleclick.net/jump/' + dclkPage + ';ord='
+ random + '">');
document.write('<img src="http://ad.doubleclick.net/ad/' + dclkPage + ';ord='
+ random + '" height=60 width=468>' + '</a>');
document.write('</iframe>');
document.write('</center></nolayer>');
document.write('<layer src="http://ad.doubleclick.net/adl/' + dclkPage +
';ord=' + random + '"></layer>');
document.write('<ilayer visibility=hide width=468 height=83></ilayer>');
}
// -->
</SCRIPT>
<!-- SCRIPT LANGUAGE="JavaScript" SRC="/global/fscript.js">
//
</SCRIPT -->
<noscript>
<p align="center">
<a href="http://ad.doubleclick.net/jump/www.codeguru.com/cpp;ord=NupbdtFCY34AAHpzggo">
<img src="http://ad.doubleclick.net/ad/www.codeguru.com/cpp;ord=NupbdtFCY34AAHpzggo"></a>
</p>
</noscript>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -