C++ Primer Plus 随记(第七章)

1.C++的函数返回值不能是数组,但可以是整数、浮点数、指针、结构或对象。可以将数组作为结构或对象的组成部分来返回。

2. int arr[];

  arr[i]==*(arr+i);  &arr[i]==arr+i;

3.将数组类型和元素数量告诉数组处理函数,用两个不同的参数传递:void fillArray(int arr[], int size); 

不要用 void fillArray (int arr[size]);

4. const Size=8;

int sum_arr(int arr[], int n);

void main(){

int cook[Size]={1,2,3,4,5,6,7,8}; cout<<sizeof cook ; //显示的长度是32
int sum=sum_arr(cook,Size); }

int sum_arr( int arr[], int n){

cout<<arr; cout<<sizeof arr; //显示的长度是4, cook和arr指向同一个地址,

                                         但sizeof cook是32(整个数组的长度),sizeof arr是4(指针变量的长度)

......

}

5.保护数组,使数组视为可读数据时,用const声明

函数要修改数组,原形:void f_modify( double arr[], int n);

函数不修改数组,原形:void f_nochange(const double arr[], int n);

8.使用数组区间的函数:

const Size=8;

int sum_arr(const int * begin, const int * end);

void main(){ 

int cook[Size]={1,2,3,4,5,6,7,8};

int sum=sum_arr(cook,  cook+Size);  }

int sum_arr(const int * begin, const int * end){

int total=0;    const int * pt;  

for (pt=begin; pt!=end; pt++)

   total=total+ *pt;

}

9.一般将指针参数声明为 指向一个常量对象的指针,不能使用指针来修改所指向的值,而能修改指针指向的位置。

int age=39;

const int * pt=&age; //指向一个常量对象的指针,不能使用指针来修改所指向age的值 ,可以使pt指向其他位置  *pt+=1; (不合法)

int * const finger=&age;//指针本身为常量,不能修改指针指向的位置,但可以用finger修改age的值

以上finger, *pt是const ,*finger 和pt 不是const.

禁止将常量数组的地址赋给非常量指针,可以使用强制类型转换来突破这种限制。(P222)

10.函数与二维数组:

int data[3][4]={ {1,2,3,4},{9,2,1,4},{2,4,6,3}};  int total=sum(data,3);

sum的原形: int sum( int (*arr2)[4] , int size); // (*arr2)[4] 表示由4个int组成的数组的指针  size表示行数 

            或者:int sum( int arr2[][4], int size);  //这两个原形arr2是指针而不是数组

( int *arr2[4] 表示由4个指向int的指针组成的数组。)

arr2

arr2+r

*(arr2+r)

*(arr2+r)+c

*(*(arr2+r))+c==arr2[r][c]

12.while (*str) 等价于 while (*str!="\0") 

#include<iostream>
char * buildstr(char c,int n);  //该函数的返回值是一个指针
int main()
{
  using namespace std;
  char ch; 
  int times;
  cout<<"Enter a character: ";
  cin>>ch;
  cout<<"Enter an integer: ";
  cin>>times;
  char * ps=buildstr(ch, times);
  cout<<ps<<endl;
  delete [] ps;  //释放指针所指内存
  ps=buildstr('+',20);  //释放后可以重新使用指针
  cout<<ps<<"Done"<<ps<<endl;
  delete [] ps;  //释放指针所指内存
  return 0;
}
char * buildstr(char c,int n) //该函数的返回值是一个指针
{
  char * pt=new char[n+1]; //用new分配动态数组
  pt[n]='\0';
  while(n-->0)
      pt[n]=c;
  return pt;
}

 13.函数与结构:函数返回的是结构

#include "stdafx.h"
#include<iostream>
struct travel_time
{
	int hour;
	int min;
};
const int mins_perh = 60;
travel_time sum(travel_time t1, travel_time t2);
void showtime(travel_time t);
using namespace std;
int main()
{
	travel_time day1 = { 5, 24 };
	travel_time day2 = { 6, 48 };
	travel_time trip = sum(day1, day2);
	cout << "Two days total: ";
	showtime(trip);

	travel_time day3= {3, 51};
	cout << "There days total: ";
	showtime(sum(trip, day3));

}
travel_time sum(travel_time t1, travel_time t2) //函数要返回一个travel_time结构,应先声明一个travel_time结构
{
	travel_time total;
	total.hour = t1.hour + t2.hour + (t1.min + t2.min) / mins_perh;
	total.min = (t1.min + t2.min) % mins_perh;
	return total;
}
void showtime(travel_time t)
{
	cout << t.hour << "hours, " << t.min << "minutes.\n";
}

传递结构地址时,函数不定义为由返回的类型比较方便

#include "stdafx.h"
#include<iostream>
#include<cmath>
using namespace std;
struct rect
{
	double x;
	double y;
};
struct polar
{
	double dis;
	double angle;
};
void rect_polar(const rect * pxy, polar * pda);
void showploar(const polar * pda);
int main()
{
	rect zb;
	polar  da;
	cout << "Enter the x and y value ";
	while (cin >> zb.x >> zb.y) //访问结构数据的成员用句点 .
	{   
		rect_polar(&zb, &da); //参数类型是指针,应对结构变量取地址
		showploar(&da);
		cout << "Next two number(q to quit): ";
	}
	return 0;
}
void rect_polar(const rect * pxy, polar * pda) //无返回值,用另一个参数来存储所需结果
{
	const double rad_to_ang = 57.29577951;
	pda->dis = sqrt(pxy->x*pxy->x + pxy->y*pxy->y); //访问结构指针的成员用->
	pda->angle = atan2(pxy->y, pxy->x)*rad_to_ang;
}
		
void showploar(const polar * pda)
{
	cout <<"distance="<< pda->dis << ", angle=" << pda->angle;
}

14. 声明string数组: string list[5];

 写入string数组:for(int i=0;i<5;i++)   getline( cin, list[i] );

15.函数的递归(P239例7.16)

16. 一个函数think() ,函数的名字think即为函数think()的地址

获取函数地址:process(think);  //传输think()函数的地址给process()

                        thought(think());  //传输think()函数的返回值给thought()

声明函数指针:double pam( int ); double (*pf) ( int ); pf=pam // pf是一个指向函数的指针 

double *pf ( int ) ;//表示pf() 返回是指针的函数

用指针调用函数: double x=pam(4);   double y=(*pf) (5); 或者 double y=pf (5);

17.在函数原型中参数列表const double ar[] 与const double *ar的含义相同。

自动类型判断auto只能用于简单的单只初始化,不能用于初始化列表。

(1)函数原型:

const double * f1(const double ar[], int n);

const double * f2(const double  [], int );

const double * f3(const double *, int );

(2)声明指向函数的指针:

const double * (*pa) (const double *, int )=f1; //声明一个指针,指向f1:

const double * (*pb[3]) (const double *, int )={ f1,f2,f3 }; // 声明一个指针数字,指向f1、f2、f3,并初始化:

pb是一个包含3个指针的 数组,每个指针指向一个函数,const double *, int作为参数,并返回一个const double *。

auto pa=pb;  //合法

(3)函数调用:

const double  *px=pb[0] (av,3); //av,3是参数    获取返回的值: double x=*pb[0] (av,3);

const double  *py= (*pb[0]) (av,3);                     获取返回的值: double y=*(*pb[0] ) (av,3);

18.创建指向整个指针数组的指针(P246例7.19)

猜你喜欢

转载自blog.csdn.net/lvliang2017232003/article/details/85759627