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

📄 all.html

📁 从www.CppReference.com打包的C++参考手册
💻 HTML
📖 第 1 页 / 共 2 页
字号:
  <div class="name-format">    rand  </div>  <div class="syntax-name-format">    Syntax:  </div>  <pre class="syntax-box">  #include &lt;stdlib.h&gt;  int rand( void );</pre>  <p>The function rand() returns a pseudorandom integer between zero  and RAND_MAX. An example:</p>  <pre class="example-code">   srand( time(<strong>NULL</strong>) );   for( i = 0; i &lt; 10; i++ )     printf( &quot;Random number #%d: %d\n&quot;, i, rand() );          </pre>  <div class="related-name-format">    Related topics:  </div>  <div class="related-content">    <a href="srand.html">srand</a>  </div>  </div>  </td>    </tr>  </table></body></html><hr>  <div class="name-format">    setjmp  </div>  <div class="syntax-name-format">    Syntax:  </div>  <pre class="syntax-box">  #include &lt;setjmp.h&gt;  int setjmp( jmp_buf envbuf );</pre>  <p>The setjmp() function saves the system stack in <em>envbuf</em>  for use by a later call to <a href="longjmp.html">longjmp</a>(). When  you first call setjmp(), its return value is zero. Later, when you  call <a href="longjmp.html">longjmp</a>(), the second argument of  <a href="longjmp.html">longjmp</a>() is what the return value of  setjmp() will be. Confused? Read about <a href=  "longjmp.html">longjmp</a>().</p>  <div class="related-name-format">    Related topics:  </div>  <div class="related-content">    <a href="longjmp.html">longjmp</a>  </div>  </div>  </td>    </tr>  </table></body></html><hr>  <div class="name-format">    signal  </div>  <div class="syntax-name-format">    Syntax:  </div>  <pre class="syntax-box">  #include &lt;signal.h&gt;  void ( *signal( int signal, void (* func) (int)) ) (int);</pre>  <p>The signal() function sets <em>func</em> to be called when  <em>signal</em> is recieved by your program. <em>func</em> can be a  custom signal handler, or one of these macros (defined in  signal.h):</p>  <table class="code-table">    <tr>      <th class="code-table-th">Macro</th>      <th class="code-table-th">Explanation</th>    </tr>    <tr>      <td class="code-table-td">SIG_DFL</td>      <td class="code-table-td">default signal handling</td>    </tr>    <tr>      <td class="code-table-td">SIG_IGN</td>      <td class="code-table-td">ignore the signal</td>    </tr>  </table>  <p>Some basic signals that you can attach a signal handler to  are:</p>  <table class="code-table">    <tr>      <th class="code-table-th">Signal</th>      <th class="code-table-th">Description</th>    </tr>    <tr>      <td class="code-table-td">SIGTERM</td>      <td class="code-table-td">Generic stop signal that can be      caught.</td>    </tr>    <tr>      <td class="code-table-td">SIGINT</td>      <td class="code-table-td">Interrupt program, normally      ctrl-c.</td>    </tr>    <tr>      <td class="code-table-td">SIGQUIT</td>      <td class="code-table-td">Interrupt program, similar to      SIGINT.</td>    </tr>    <tr>      <td class="code-table-td">SIGKILL</td>      <td class="code-table-td">Stops the program. Cannot be      caught.</td>    </tr>    <tr>      <td class="code-table-td">SIGHUP</td>      <td class="code-table-td">Reports a disconnected terminal.</td>    </tr>  </table>  <p>The return value of signal() is the address of the previously  defined function for this signal, or SIG_ERR is there is an  error.</p>  <div class="related-examples-format">    Example code:  </div>  <div class="related-examples">    <p>The following example uses the signal() function to call an    arbitrary number of functions when the user aborts the program. The    functions are stored in a vector, and a single &quot;clean-up&quot;    function calls each function in that vector of functions when the    program is aborted:</p>    <pre class="example-code">void f1() {  cout &lt;&lt; &quot;calling f1()...&quot; &lt;&lt; endl;}               void f2() {  cout &lt;&lt; &quot;calling f2()...&quot; &lt;&lt; endl;}               typedef void(*endFunc)(void);vector&lt;endFunc&gt; endFuncs;         void cleanUp( int dummy ) {  for( unsigned int i = 0; i &lt; endFuncs.size(); i++ ) {    endFunc f = endFuncs.at(i);    (*f)();  }  exit(-1);}               int main() {              // connect various signals to our clean-up function  signal( SIGTERM, cleanUp );  signal( SIGINT, cleanUp );  signal( SIGQUIT, cleanUp );  signal( SIGHUP, cleanUp );              // add two specific clean-up functions to a list of functions  endFuncs.push_back( f1 );  endFuncs.push_back( f2 );               // loop until the user breaks  while( 1 );             return 0;}               </pre>  </div>  <div class="related-name-format">    Related topics:  </div>  <div class="related-content">    <a href="raise.html">raise</a>  </div>  </div>  </td>    </tr>  </table></body></html><hr>  <div class="name-format">    srand  </div>  <div class="syntax-name-format">    Syntax:  </div>  <pre class="syntax-box">  #include &lt;stdlib.h&gt;  void srand( unsigned seed );</pre>  <p>The function srand() is used to seed the random sequence generated  by <a href="rand.html">rand</a>(). For any given <em>seed</em>,  rand() will generate a specific &quot;random&quot; sequence over and  over again.</p>  <pre class="example-code">   srand( time(<strong>NULL</strong>) );   for( i = 0; i &lt; 10; i++ )     printf( &quot;Random number #%d: %d\n&quot;, i, rand() );          </pre>  <div class="related-name-format">    Related topics:  </div>  <div class="related-content">    <a href="rand.html">rand</a><br>    (Standard C Date &amp; Time) <a href=    "../stddate/time.html">time</a>  </div>  </div>  </td>    </tr>  </table></body></html><hr>  <div class="name-format">    system  </div>  <div class="syntax-name-format">    Syntax:  </div>  <pre class="syntax-box">  #include &lt;stdlib.h&gt;  int system( const char *command );</pre>  <p>The system() function runs the given <em>command</em> by passing  it to the default command interpreter.</p>    <p>The return value is usually zero if the command executed without  errors. If <em>command</em> is <strong>NULL</strong>, system() will  test to see if there is a command interpreter available. Non-zero  will be returned if there is a command interpreter available, zero  if not.</p>  <div class="related-name-format">    Related topics:  </div>  <div class="related-content">    <a href="exit.html">exit</a><br>    <a href="getenv.html">getenv</a>  </div>  </div>  </td>    </tr>  </table></body></html><hr>  <div class="name-format">    va_arg  </div>  <div class="syntax-name-format">    Syntax:  </div>  <pre class="syntax-box">  #include &lt;stdarg.h&gt;  type va_arg( <strong>va_list</strong> argptr, type );  void va_end( <strong>va_list</strong> argptr );  void va_start( <strong>va_list</strong> argptr, last_parm );</pre>  <p>The va_arg() macros are used to pass a variable number of  arguments to a function.</p>  <ol>    <li>First, you must have a call to va_start() passing a valid    <strong>va_list</strong> and the mandatory first argument of the    function. This first argument can be anything; one way to use it is    to have it be an integer describing the number of parameters being    passed.</li>    <li>Next, you call va_arg() passing the <strong>va_list</strong>    and the type of the argument to be returned. The return value of    va_arg() is the current parameter.</li>    <li>Repeat calls to va_arg() for however many arguments you    have.</li>    <li>Finally, a call to va_end() passing the    <strong>va_list</strong> is necessary for proper cleanup.</li>  </ol>  <p>For example:</p>  <pre class="example-code">  int sum( int num, ... ) {    int answer = 0;    <strong>va_list</strong> argptr;                va_start( argptr, num );                for( ; num &gt; 0; num-- ) {      answer += va_arg( argptr, int );    }               va_end( argptr );               return( answer );  }                               int main( void ) {                int answer = sum( 4, 4, 3, 2, 1 );    printf( &quot;The answer is %d\n&quot;, answer );               return( 0 );  }             </pre>  <p>This code displays 10, which is 4+3+2+1.</p>  <p>Here is another example of variable argument function, which is a  simple printing function:</p>  <pre class="example-code"> void my_printf( char *format, ... ) {   <strong>va_list</strong> argptr;                va_start( argptr, format );             while( *format != &#39;\0&#39; ) {     // string     if( *format == &#39;s&#39; ) {       char* s = va_arg( argptr, char * );       printf( &quot;Printing a string: %s\n&quot;, s );     }     // character     else if( *format == &#39;c&#39; ) {       char c = (char) va_arg( argptr, int );       printf( &quot;Printing a character: %c\n&quot;, c );       break;     }     // integer     else if( *format == &#39;d&#39; ) {       int d = va_arg( argptr, int );       printf( &quot;Printing an integer: %d\n&quot;, d );     }               *format++;   }               va_end( argptr ); }                               int main( void ) {                my_printf( &quot;sdc&quot;, &quot;This is a string&quot;, 29, &#39;X&#39; );            return( 0 ); }              </pre>  <p>This code displays the following output when run:</p>  <pre class="example-code"> Printing a string: This is a string Printing an integer: 29 Printing a character: X                </pre>  </div>  </td>    </tr>  </table></body></html><hr></body></html>

⌨️ 快捷键说明

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