blob: 043cbeb36dd1e66d369ecef695227353a9e9df94 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Generalized Operations</title><meta name="generator" content="DocBook XSL-NS Stylesheets V1.76.1"/><meta name="keywords" content=" ISO C++ , library "/><link rel="home" href="../spine.html" title="The GNU C++ Library"/><link rel="up" href="numerics.html" title="Chapter 12. Numerics"/><link rel="prev" href="numerics.html" title="Chapter 12. Numerics"/><link rel="next" href="numerics_and_c.html" title="Interacting with C"/></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Generalized Operations</th></tr><tr><td align="left"><a accesskey="p" href="numerics.html">Prev</a> </td><th width="60%" align="center">Chapter 12.
Numerics
</th><td align="right"> <a accesskey="n" href="numerics_and_c.html">Next</a></td></tr></table><hr/></div><div class="section" title="Generalized Operations"><div class="titlepage"><div><div><h2 class="title"><a id="std.numerics.generalized_ops"/>Generalized Operations</h2></div></div></div><p>
</p><p>There are four generalized functions in the <numeric> header
that follow the same conventions as those in <algorithm>. Each
of them is overloaded: one signature for common default operations,
and a second for fully general operations. Their names are
self-explanatory to anyone who works with numerics on a regular basis:
</p><div class="itemizedlist"><ul class="itemizedlist"><li class="listitem"><p><code class="code">accumulate</code></p></li><li class="listitem"><p><code class="code">inner_product</code></p></li><li class="listitem"><p><code class="code">chapterial_sum</code></p></li><li class="listitem"><p><code class="code">adjacent_difference</code></p></li></ul></div><p>Here is a simple example of the two forms of <code class="code">accumulate</code>.
</p><pre class="programlisting">
int ar[50];
int someval = somefunction();
// ...initialize members of ar to something...
int sum = std::accumulate(ar,ar+50,0);
int sum_stuff = std::accumulate(ar,ar+50,someval);
int product = std::accumulate(ar,ar+50,1,std::multiplies<int>());
</pre><p>The first call adds all the members of the array, using zero as an
initial value for <code class="code">sum</code>. The second does the same, but uses
<code class="code">someval</code> as the starting value (thus, <code class="code">sum_stuff == sum +
someval</code>). The final call uses the second of the two signatures,
and multiplies all the members of the array; here we must obviously
use 1 as a starting value instead of 0.
</p><p>The other three functions have similar dual-signature forms.
</p></div><div class="navfooter"><hr/><table width="100%" summary="Navigation footer"><tr><td align="left"><a accesskey="p" href="numerics.html">Prev</a> </td><td align="center"><a accesskey="u" href="numerics.html">Up</a></td><td align="right"> <a accesskey="n" href="numerics_and_c.html">Next</a></td></tr><tr><td align="left" valign="top">Chapter 12.
Numerics
</td><td align="center"><a accesskey="h" href="../spine.html">Home</a></td><td align="right" valign="top"> Interacting with C</td></tr></table></div></body></html>
|