C++ Primer Plus Notes (Chapter 7)

1. The return value of a C++ function cannot be an array, but it can be an integer, floating-point number, pointer, structure or object. You can return an array as part of a structure or object.

2. int arr[];

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

3. Tell the array type and number of elements to the array processing function, and pass them with two different parameters: void fillArray(int arr[], int size); 

Do not use 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; //The displayed length is 32
int sum=sum_arr(cook,Size);}

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

cout<<arr; cout<<sizeof arr; //The length displayed is 4, cook and arr point to the same address,

                                         But sizeof cook is 32 (length of the entire array), sizeof arr is 4 (length of pointer variable)

......

}

5. When protecting the array and making the array as readable data, declare it with const

The function needs to modify the array, the original shape: void f_modify( double arr[], int n);

The function does not modify the array, the original shape: void f_nochange(const double arr[], int n);

8. Use the function of the array interval:

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. General will refer to pin point to a constant parameter is declared as a pointer to the object can not be used to modify the value of the pointer is pointing, but can modify the position pointer.

int age=39;

const int * pt=&age; //Pointer to a constant object, pointers cannot be used to modify the value of age pointed to, and pt can be made to point to other locations *pt+=1; (illegal)

int * const finger=&age; //The pointer itself is a constant, you cannot modify the position pointed to by the pointer, but you can use finger to modify the value of age

Above finger, *pt are const, *finger and pt are not const.

It is forbidden to assign the address of a constant array to a non-constant pointer, and you can use cast to break this limitation. (P222)

10. Functions and two-dimensional arrays:

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

The original form of sum: int sum( int (*arr2)[4], int size); //  (*arr2)[4] represents the pointer of an array composed of 4 ints,   size represents the number of rows 

            Or: int sum( int arr2[][4], int size); //These two prototypes arr2 are pointers rather than arrays

(Int *arr2[4] represents an array composed of 4 pointers to 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. Function and structure: function returns structure

#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";
}

When passing the structure address, it is more convenient for the function not to be defined as the type returned

#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. Declare the string array: string list[5];

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

15. Function recursion (P239 example 7.16)

16. A function think(), the name of the function think is the address of the function think()

Get the function address: process(think); //Transmit the address of the think() function to process()

                        thought(think()); //Transfer the return value of the think() function to thought()

Declare function pointer: double pam( int ); double (*pf) (int ); pf=pam // pf is a pointer to a function 

double *pf (int) ;//Indicates that pf() returns a pointer function

Call a function with a pointer: double x=pam(4); double y=(*pf) (5); or double y=pf (5);

17. The parameter list const double ar[] in the function prototype has the same meaning as const double *ar.

Automatic type judgment auto can only be used for simple single initialization, not for initialization lists.

(1) Function prototype:

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

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

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

(2) Declare pointers to functions:

const double * (*pa) (const double *, int )=f1; //Declare a pointer to f1:

const double * (*pb[3]) (const double *, int )={ f1,f2,f3 }; // Declare a pointer number, point to f1, f2, f3, and initialize:

pb is an array containing 3 pointers, each pointer points to a function, const double *, int as parameters, and returns a const double *.

auto pa=pb; //legal

(3) Function call:

const double *px=pb[0] (av,3); //av,3 is the parameter Get the returned value: double x=*pb[0] (av,3);

const double *py= (*pb[0]) (av,3); Get the returned value: double y=*(*pb[0]) (av,3);

18. Create a pointer to the entire pointer array (P246 Example 7.19)

Guess you like

Origin blog.csdn.net/lvliang2017232003/article/details/85759627