⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fun_5137.htm

📁 C++标准库 C++标准库 C++标准库 C++标准库
💻 HTM
字号:
<HTML><HEAD><TITLE>3.1 Functions</TITLE></HEAD><BODY><A HREF="ug1.htm"><IMG SRC="images/banner.gif"></A><BR><A HREF="fun_1852.htm"><IMG SRC="images/prev.gif"></A><A HREF="booktoc1.htm"><IMG SRC="images/toc.gif"></A><A HREF="tindex1.htm"><IMG SRC="images/tindex.gif"></A><A HREF="pre_1157.htm"><IMG SRC="images/next.gif"></A><BR><STRONG>Click on the banner to return to the user guide home page.</STRONG><H2>3.1 Functions</H2><A NAME="idx27"><!></A><P>A number of algorithms provided in the standard library require functions as arguments.  A simple example is the algorithm <SAMP>for_each(),</SAMP> which invokes a function, passed as an argument, on each value held in a container.  The following, for example, applies the <SAMP>printElement()</SAMP> function to produce output describing each element in a list of integer values:</P><PRE>void printElement (int value){   cout &#60;&#60; "The list contains " &#60;&#60; value &#60;&#60; endl;}main () {   list&#60;int> aList;      ...   for_each (aList.begin(), aList.end(), printElement);}</PRE><P><I>Binary functions</I> take two arguments, and are often applied to values from two different sequences.  For example, suppose we have a list of strings and a list of integers.  For each element in the first list we wish to replicate the string the number of times given by the corresponding value in the second list.  We could perform this easily using the function <SAMP>transform()</SAMP> from the standard library.  First, we define a binary function with the desired characteristics:</P><PRE>string stringRepeat (const string &#38; base, int number)                    // replicate base the given number of times{   string result;   // initially the result is empty   while (number--)  result += base;   return result;}</PRE><P>The following call on <SAMP>transform()</SAMP> then produces the desired effect:</P><PRE>list&#60;string> words;list&#60;int> counts;   ...transform (words.begin(), words.end(),    counts.begin(), words.begin(), stringRepeat);</PRE><P>Transforming the words <SAMP>one, two, three</SAMP> with the values 3, 2, 3 would yield the result <SAMP>oneoneone, twotwo, threethreethree</SAMP>.</P><HR><A HREF="fun_1852.htm"><IMG SRC="images/prev.gif"></A> <A HREF="booktoc1.htm"><IMG SRC="images/toc.gif"></A><A HREF="tindex1.htm"><IMG SRC="images/tindex.gif"></A><A HREF="pre_1157.htm"><IMG SRC="images/next.gif"></A><P>&copy;Copyright 1996, Rogue Wave Software, Inc.</P></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -