C++学习第二课

来源:互联网 发布:凸优化求解算法 编辑:程序博客网 时间:2024/06/05 00:35

  今天会把我学习过的知识都总结下来,有一些会简要带过,重点记录那些让我豁然开朗的内容。PS,马上就要开题了,希望自己好好加油!

  类型限定符:const(恒定) volatile(不稳定的) restrict(指针类型,唯一不重叠)

  存储类型符:auto register static extern mutable

  操作符:       +  -   *  /  %  ++   --   ==  !=   <  >   <=   >=   =   +=   -=   *=   /=    %=   &&  ||   !   &  |   ^   <<   >>   ~ 

                        sizeof  condition? X:Y   ,(comma)   cast   .(dot)  ->(arrow)   *(pointer)  &(address)

                        eg: cast  强制转换类型   int(2.2200)=2

  循环:           while()   do{ }while( )   for( ; ; ){ }  break  continue   goto  

  条件判断:    switch(){ case1:0  break;}   if   if else

  函数调用:    call by value,call by pointer,call by reference

                        指针调用是C和C++共有的,引用调用是C++所特有的,效果等同于指针调用,可以改变被调用的量,一般都采用这种方式,指针调用易出错(不容易检查出来)

下面是引用调用实例:

 

#include <iostream>using namespace std;int swap(int &,int &);int main(){  int a=3,b=4;  swap(a,b);  cout << "a:" << a << endl;  cout << "b:" << b << endl;  return 0;} int swap(int &x,int &y){  int rent;  rent=x;   x=y;  y=rent;  return 0;}

a,b显示的最终结果分别是4,3,这表明引用调用可以改变被调用的值。


指针调用:

#include <iostream>using namespace std;void swap(int *,int *);int main(){  int a=3,b=4;  swap(&a,&b);  cout << "a:" << a << endl;  cout << "b:" << b << endl;  return o;} void swap(int *x,int *y){  int rent;  rent=*x;   *x=*y;  *y=rent;  return 0;}

数学表达式:

cos  sin  tan   log(底数为e)   pow   hypot   sqrt  abs  fabs  floor(取整)


随机数的产生:

#include <iostream>#include <ctime>#include <cstdlib>using namespace std; int main (){   int i,j;    // set the seed   srand( (unsigned)time( NULL ) );   /* generate 10  random numbers. */   for( i = 0; i < 10; i++ )   {      // generate actual random number      j= rand();      cout <<" Random Number : " << j<< endl;   }   return 0;}


<cstdlib>包含rand,srand函数,产生随机函数必须得有一个种子,用srand产生。为了使每次产生的随机数不一样,需要利用到当前时间。

倘若srand()一样,则产生的随机数是一样的,如下图:

#include <iostream>#include <cstdlib>using namespace std;int main(){   int i,j;   for(i=0;i<10;i++)   {      srand(1);      j=rand();      cout << j;   }   return 0;}

倘若 srand(1)置于int i,j 的上一行,则产生的随机数也是不一样的。

                       

             


0 0
原创粉丝点击