These built-in functions commonly used in C ++ you a few? ?

Foreword

Mathematical Functions
C ++ built a wealth of mathematical functions, can operate on a variety of digital. The following table lists the C ++ some useful built-in mathematical functions.
To use these functions, you need to reference the math header file.

function description
double cos(double); This function returns the angle in radians (double type) (degrees) cosine.
double sin(double); This function returns the angle in radians (double type) (degrees) sinusoidal.
double tan(double); This function returns the angle in radians (double type) (degrees) of the tangent.
double log(double); This function returns the natural logarithm.
double pow(double, double); The first parameter is assumed as x, the second argument is y, then the function returns the y-th power of x.
double hypot(double, double); This function returns the square root of the square sum of the two parameters, i.e., two parameters are at right angles to a side of the right triangle, the function returns the length of the hypotenuse.
double sqrt(double); This function returns the square root of the argument.
int abs(int); This function returns the absolute value of an integer.
double fabs(double); This function returns the absolute value of any of a decimal number.
double floor(double); This function returns the largest integer less than or equal to the arguments passed.

Random function
in many cases, it is necessary to generate a random number. Random number generator, there are two related functions. Is a rand (), the function returns a pseudo-random number only. You must call srand () function before generating a random number.
The following is a simple example on random number generation. Examples of the use time () function to obtain the number of seconds the system time, the function generates a random number by calling the rand ():

#include <iostream>
#include <ctime>
#include <cstdlib>
 
using namespace std;
 
int main ()
{
   int i,j;
 
   // 设置种子
   srand( (unsigned)time( NULL ) );
 
   /* 生成 10 个随机数 */
   for( i = 0; i < 10; i++ )
   {
      // 生成实际的随机数
      j= rand();
      cout <<"随机数: " << j << endl;
   }
 
   return 0;
}

 

When the above code is compiled and executed, it produces the following results:

随机数: 1748144778
随机数: 630873888
随机数: 2134540646
随机数: 219404170
随机数: 902129458
随机数: 920445370
随机数: 1319072661
随机数: 257938873
随机数: 1256201101
随机数: 58032298

setw () function
in C ++, setw (int n) for controlling the output interval.
E.g:

cout<<'s'<<setw(8)<<'a'<<endl;

The on-screen display

s       a

Between a 7 // s and spaces, setw () followed by its output only an effect, the above embodiment shows 'a' representing a total of eight positions, with spaces underfill. If the content of the input exceeds setw () length at the actual length of the output set.
setw () default content-filled spaces, you can use setfill () with settings for other characters fill.
As
cout<<setfill('*')<<setw(5)<<'a'<<endl;
the output:
****a //4个*和字符a共占5个位置。

The so-called wide-field, is the content of the output (value or character, etc.) need to occupy the position of the number of characters, if there is vacant position will be automatically filled. For example, we want to set the field width is 2, then the digit 1 when the output of the output is "1", that is in front of one plus one space. A space and the number 1 position occupied by a total of just two characters. If you do not learn C ++, C ++ can get my latest tutorial succinctly point me into the dress itself acquired,

We set the field width and padding character when the points to note:
① set up a domain-wide time should be filled integer, set the fill character should be filled in when the character.
② We can also set the contents of a field to be output width and padding characters, but set a good property only for the next output content is valid, then the output to be set again. I.e. cout << setw (2) << a << b; statement is only provided in a wide-field effective in the b invalid.
③setw and setfill output control characters are called, need to write at the beginning of the program #include "iomanip.h" use, or not use.

Examples

#include <iostream>
using namespace std;
 
#include <iomanip>
using std::setw;
 
int main ()
{
   int n[ 10 ]; // n 是一个包含 10 个整数的数组
 
   // 初始化数组元素          
   for ( int i = 0; i < 10; i++ )
   {
      n[ i ] = i + 100; // 设置元素 i 为 i + 100
   }
   cout << "Element" << setw( 13 ) << "Value" << endl;
 
   // 输出数组中每个元素的值                     
   for ( int j = 0; j < 10; j++ )
   {
      cout << setw( 7 )<< j << setw( 13 ) << n[ j ] << endl;
   }
 
   return 0;
}

The above procedure was used setw () function to format output. When the above code is compiled and executed, it produces the following results:

Element        Value
      0          100
      1          101
      2          102
      3          103
      4          104
      5          105
      6          106
      7          107
      8          108
      9          109

 

 

Guess you like

Origin www.cnblogs.com/chengxuyuanaa/p/11962679.html