C++学习第二课

  今天会把我学习过的知识都总结下来,有一些会简要带过,重点记录那些让我豁然开朗的内容。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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值