C++快速温习笔记(基本)[2]

103 篇文章 0 订阅
7 篇文章 0 订阅


常用数学运算函数:

函数

描述

double cos(double)

该函数返回弧度角(double 型)的余弦

double sin(double)

该函数返回弧度角(double 型)的正弦

double tan(double)

该函数返回弧度角(double 型)的正切

double log(double)

该函数返回参数的自然对数

double pow(double, double)

假设第一个参数为 x,第二个参数为 y,则该函数返回 x y 次方

double hypot(double, double)

该函数返回两个参数的平方总和的平方根,也就是说,参数为一个直角三角形的两个直角边,函数会返回斜边的长度

double sqrt(double)

该函数返回参数的平方根

int abs(int)

该函数返回整数的绝对值

double fabs(double)

该函数返回任意一个十进制数的绝对值

double floor(double)

该函数返回一个小于或等于传入参数的最大整数

 

生成随机数:

srand( (unsigned)time( NULL ));    //设置随机数种子

a = rand();    //生成随机数

 

声明数组:

typearrayName [ arraySize ];

e.g.

double numbers[10];

double numbers[5] = { 10.0, 3.1415,2.2, 1,1, 3,3 };

double numbers[] = { 10.0, 3.1415,2.2, 1,1, 3,3 };

多维数组:

 type name[size1][size2]...[sizeN];

e.g.

int arr[5][10];

int arr[2][2] = { {0,1}, {2,3} };

int arr]2][2] = { 0, 1, 2, 3 };

 

字符串操作函数:

函数

描述

strcpy(s1,s2)

复制字符串2到字符串1

strcat(s1,s2)

连接字符串s2到字符串s1末尾

strlen(s1)

返回字符串s1的长度

strcmp(s1,s2)

如果s1s2相同,则返回0,如果s1<s2,则返回小于0,反之,大于0

strchr(s1,ch)

返回一个指针,指向字符串s1中第一次出现字符ch的位置

strstr(s1,s2)

返回一个指针,指向字符串s1中第一次出现字符串s2的位置

 

如果指针不需要继续使用或者暂时不指定地址,一个好的变成习惯是将其指到NULL上:

int *ptr = NULL

使用&获取指针的起始地址,数组的名称是一个指针:

int var[MAX] = {10, 100, 200};

int *ptr;

ptr = var;

for (int i = 0; i < MAX; i++)

{

  cout << "var[" << i << "]的内存地址为 ";

  cout << ptr << endl;

  cout << "var[" << i << "] 的值为 ";

  cout << *ptr << endl;

  ptr++;

}

指向指针的指针:

int var = 10;

int *ptr;

ptr = &var;

int **pptr;

pptr = &ptr;

访问指针指向的地址的内容,使用*:

cout <<var;    //10

cout <<ptr;    //address of var

cout <<*ptr;    //value of var, 10

cout <<pptr;    //address of ptr

cout <<*pptr;    //value of ptr(address of var)

cout <<**pptr;    //value of var, 10

引用的用法:

int a = 10;

int &b = a;    //ba的引用,可以看成b就是a

指针和引用的区别:

(1)指针:指针是一个变量,只不过这个变量存储的是一个地址,指向内存的一个存储单元;而引用跟原来的变量实质上是同一个东西,只不过是原变量的一个别名而已。

(2)可以有const指针,但是没有const引用;

(3)指针可以有多级,但是引用只能是一级(int**p;合法 而 int &&a是不合法的)

(4)指针的值可以为空,但是引用的值不能为NULL,并且引用在定义的时候必须初始化;

(5)指针的值在初始化后可以改变,即指向其它的存储单元,而引用在进行初始化后就不会再改变了。

(6)"sizeof引用"得到的是所指向的变量(对象)的大小,而"sizeof指针"得到的是指针本身的大小;

(7)指针和引用的自增(++)运算意义不一样;

 

看两个小程序:

void test(int *p)

{

  int a=1;

  p=&a;

}

 

int main(void)

{

    int *p=NULL;

    test(p);

    // p =NULL,原因是当调用test函数时,事实上传递的也是地址,只不过传递的是指针地址。也就是说将指针作为参数进行传递时,事实上也是值传递。

   

    return 0;

}

void test(int*&p)

{

  int a=1;

  p=&a;

  cout<<p<<""<<*p<<endl;

}

修改test函数:

void test(int *&p)    //这里参数的传值是引用传递 

{

  int a=1;

  p=&a;

}

这次,使用引用传递就可以修改参数的值了。

 

使用指针作为参数的swap方法:

 void swap(int*a, int *b)

{

    int temp = *a;

    *a = *b;

    *b = temp;

}

int a = 1;

int b = 2;

swap(&a,&b);

cout << a<< b;    //result: 2 1

 

使用引用作为参数的swap方法:

void swap(int&a, int &b)

{

    int temp = a;

    a = b;

    b = temp;
}

int a = 1;

int b = 2;

swap(&a,&b);

cout << a<< b;    //result: 2 1

 

使用引用作为函数返回值可以返回一个指向返回值的隐式指针,例如:

int a[] = { 1, 2,3, 4 };

int&GetValue(int index)

{

    return a[i];
}

//a[2] = 3;

GetValue(3) = 1000;

//a[2] = 1000

注意:当返回值为引用类型时,被引用的对象不能超出其作用域,所以返回局部变量是不合法的。但是可以返回对静态变量的引用:

int& func() {

   int q;

   //! return q; // 在编译时发生错误

   static int x;

   return x;    // 安全,x 在函数作用域外依然是有效的

}

 

C++标准库中并没有提供日期/时间类型,而是继承了C语言中定义的用于时间和日期操作的结构和函数。需要在头部引用<ctime>头文件。

有四个与时间相关的类型:clock_t、time_t、size_t和 tm。类型 clock_t、size_t 和 time_t 能够把系统时间和日期表示为某种整数。

tm结构的定义如下:

struct tm {

  int tm_sec;  // 秒,正常范围从 0 到 59,但允许至 61

  int tm_min;  // 分,范围从 0 到 59

  int tm_hour; // 小时,范围从 0 到 23

  int tm_mday; // 一月中的第几天,范围从 1 到 31

  int tm_mon;  // 月,范围从 0 到 11

  int tm_year; // 自 1900 年起的年数

  int tm_wday; // 一周中的第几天,范围从 0 到 6,从星期日算起

  int tm_yday; // 一年中的第几天,范围从 0 到 365,从 1 月 1 日算起

  int tm_isdst; // 夏令时

}

 

时间相关函数:

函数

描述

简要示例

time_t time(time_t* time)

该函数返回系统的当前日历时间,自 1970 1 1 日以来经过的秒数。如果系统没有时间,则返回 .1

time_t seconds = time(NULL);

printf("自 1970-01-01 起的小时数 = %ld\n", seconds/3600);

char* ctime(const time_t* time)

该函数返回一个表示当地时间的字符串指针,字符串形式 day month year hours:minutes:seconds year\n\0

time_t currentTime;

time(&currentTime);

printf("当前时间 = %s", ctime(&currentTime));

struct tm* localtime(const time_t* time)

该函数返回一个指向表示本地时间的 tm 结构的指针。

time_t rawtime;

time(&rawtime);

struct tm* info = localtime(&rawtime);

printf("当前的本地时间和日期:%s", asctime(info));

clock_t clock(void)

该函数返回程序执行起(一般为程序的开头),处理器时钟所使用的时间。如果时间不可用,则返回 .1

time_t start, end;

start = clock();

for(int i=0; i< 100000000; i++) { }

end = clock();

printf("Total CPU time : %fs", (double)(end - start) / CLOCKS_PER_SEC);

char *asctime(const struct tm *timeptr)

该函数返回一个指向字符串的指针,字符串包含了 time 所指向结构中存储的信息,返回形式为:day month date hours:minutes:seconds year\n\0

localtime方法示例

struct tm *gmtime(const time_t *timer)

使用 timer 的值来填充 tm 结构,并用协调世界时(UTC)也被称为格林尼治标准时间(GMT)表示。

time_t rawtime;

struct tm* info;

time(&rawtime);

info = gmtime(&rawtime);

printf("伦敦:%2d:%02d\n", (info->tm_hour+1)%24, info->tm_min);

printf("中国:%2d:%02d\n", (info->tm_hour+8)%24, info->tm_min);

time_t mktime(struct tm *timeptr)

timeptr 所指向的结构转换为一个依据本地时区的 time_t 值。

struct tm info;

info.tm_year = 2001 - 1900;  //算自1900年起的

info.tm_mon = 7 - 1; //7月,起始是0

info.tm_mday = 4;

info.tm_hour = 0;

info.tm_min = 0;

info.tm_sec = 1;

info.tm_isdst = -1;

int ret = mktime(&info);

double difftime(time_t time1, time_t time2)

返回 time1 time2 之间相差的秒数 (time1 - time2)

time_t start, end;

start = time(NULL);

for(int i = 0; i < 1500000000; i++) { }

end = time(NULL);

printf("Passed seconds: %f", difftime(end, start));

size_t strftime(char *str, size_t maxsize, const char *format, const struct tm *timeptr)

根据 format 中定义的格式化规则,格式化结构 timeptr 表示的时间,并把它存储在 str 中。

time_t rawtime;

struct tm *info;

char buffer[80];

time(&rawtime);

info = localtime(&rawtime);

strftime(buffer, 80, "%x - %I:%M%p", info);

printf("格式化的日期 & 时间 : |%s|\n", buffer);

 

strftime格式化规则:

说明符

替换符

实例

%a

缩写的星期几名称

Sun

%A

完整的星期几名称

Sunday

%b

缩写的月份名称

Mar

%B

完整的月份名称

March

%c

日期和时间表示法

Sun Aug 19 02:56:02 2012

%d

一月中的第几天(01-31

19

%H

24 小时格式的小时(00-23

14

%I

12 小时格式的小时(01-12

05

%j

一年中的第几天(001-366

231

%m

十进制数表示的月份(01-12

08

%M

分(00-59

55

%p

AM 或 PM 名称

PM

%S

秒(00-61

02

%U

一年中的第几周,以第一个星期日作为第一周的第一天(00-53

33

%w

十进制数表示的星期几,星期日表示为 00-6

4

%W

一年中的第几周,以第一个星期一作为第一周的第一天(00-53

34

%x

日期表示法

08/19/12

%X

时间表示法

02:50:06

%y

年份,最后两个数字(00-99

01

%Y

年份

2012

%Z

时区的名称或缩写

CDT

%%

一个 % 符号

%

 

与IO相关的头文件:

头文件

说明

<iostream>

该文件定义了 cincoutcerr clog 对象,分别对应于标准输入流、标准输出流、标准错误流和标准日志流。

<iomanip>

该文件通过所谓的参数化的流操纵器(比如 setw setprecision),来声明对执行标准化 I/O 有用的服务。

<fstream>

该文件为用户控制的文件处理声明服务。

 

cerr标准错误流的用法:

预定义的对象 cerr 是ostream 类的一个实例。cerr 对象附属到标准错误设备,通常也是显示屏,但是 cerr 对象是非缓冲的,且每个流插入到 cerr 都会立即输出。

char str[] ="Error: some error....";

cerr <<"Error message : " << str << endl;

 

clog标准日志流的用法:

预定义的对象 clog 是ostream 类的一个实例。clog 对象附属到标准错误设备,通常也是显示屏,但是 clog 对象是缓冲的。这意味着每个流插入到 clog都会先存储在缓冲在,直到缓冲填满或者缓冲区刷新时才会输出。

char str[] ="Error: some error....";

clog <<"Error message : " << str << endl;

 

通过这些小实例,无法区分cout、cerr 和 clog 的差异,但在编写和执行大型程序时,它们之间的差异就变得非常明显。所以良好的编程实践告诉我们,使用 cerr流来显示错误消息,而其他的日志消息则使用 clog 流来输出。

 

定义结构:

struct [structuretag]

{

   member definition;

   member definition;

   ...

   member definition;

} [one or morestructure variables];

函数参数:

void funcName (struct struct_name a);

void funcName(struct struct_name* a);

使用typedef的写法,例如:

typedef struct

{

   char title[50];

   char author[50];

   char subject[100];

   int  book_id;

}Books;

Books Book1, Book2;

  • 81
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值