姓名:張立斐 ? 學(xué)號(hào):19020700001? ?學(xué)院:電子工程學(xué)院?
轉(zhuǎn)自:https://blog.csdn.net/lvliang2017232003/article/details/85759627
【嵌牛導(dǎo)讀】C++學(xué)習(xí)
【嵌牛鼻子】C++
【嵌牛提問(wèn)】如何學(xué)習(xí)C++?
【嵌牛正文】
1.C++的函數(shù)返回值不能是數(shù)組虎谢,但可以是整數(shù)饱岸、浮點(diǎn)數(shù)李命、指針、結(jié)構(gòu)或?qū)ο笙彼?梢詫?shù)組作為結(jié)構(gòu)或?qū)ο蟮慕M成部分來(lái)返回。
2. int arr[];
? arr[i]==*(arr+i);? &arr[i]==arr+i;
3.將數(shù)組類型和元素?cái)?shù)量告訴數(shù)組處理函數(shù)奶卓,用兩個(gè)不同的參數(shù)傳遞: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 ; //顯示的長(zhǎng)度是32
int sum=sum_arr(cook,Size); }
int sum_arr( int arr[], int n){
cout<<arr; cout<<sizeof arr; //顯示的長(zhǎng)度是4, cook和arr指向同一個(gè)地址陵珍,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 但sizeof cook是32(整個(gè)數(shù)組的長(zhǎng)度)寝杖,sizeof arr是4(指針變量的長(zhǎng)度)
......
}
5.保護(hù)數(shù)組,使數(shù)組視為可讀數(shù)據(jù)時(shí)互纯,用const聲明
函數(shù)要修改數(shù)組瑟幕,原形:void f_modify( double arr[], int n);
函數(shù)不修改數(shù)組,原形:void f_nochange(const double arr[], int n);
8.使用數(shù)組區(qū)間的函數(shù):
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.一般將指針參數(shù)聲明為 指向一個(gè)常量對(duì)象的指針,不能使用指針來(lái)修改所指向的值只盹,而能修改指針指向的位置辣往。
int age=39;
const int * pt=&age; //指向一個(gè)常量對(duì)象的指針,不能使用指針來(lái)修改所指向age的值 殖卑,可以使pt指向其他位置? *pt+=1; (不合法)
int * const finger=&age;//指針本身為常量站削,不能修改指針指向的位置,但可以用finger修改age的值
以上finger, *pt是const ,*finger 和pt 不是const.
禁止將常量數(shù)組的地址賦給非常量指針孵稽,可以使用強(qiáng)制類型轉(zhuǎn)換來(lái)突破這種限制许起。(P222)
10.函數(shù)與二維數(shù)組:
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個(gè)int組成的數(shù)組的指針? size表示行數(shù)
? ? ? ? ? ? 或者:int sum( int arr2[][4], int size);? //這兩個(gè)原形arr2是指針而不是數(shù)組
( int *arr2[4] 表示由4個(gè)指向int的指針組成的數(shù)組。)
arr2
arr2+r
*(arr2+r)
*(arr2+r)+c
*(*(arr2+r))+c==arr2[r][c]
12.while (*str) 等價(jià)于 while (*str!="\0")
#include<iostream>
char * buildstr(char c,int n);? //該函數(shù)的返回值是一個(gè)指針
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;? //釋放指針?biāo)竷?nèi)存
? ps=buildstr('+',20);? //釋放后可以重新使用指針
? cout<<ps<<"Done"<<ps<<endl;
? delete [] ps;? //釋放指針?biāo)竷?nèi)存
? return 0;
}
char * buildstr(char c,int n) //該函數(shù)的返回值是一個(gè)指針
{
? char * pt=new char[n+1]; //用new分配動(dòng)態(tài)數(shù)組
? pt[n]='\0';
? while(n-->0)
? ? ? pt[n]=c;
? return pt;
}
13.函數(shù)與結(jié)構(gòu):函數(shù)返回的是結(jié)構(gòu)
#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) //函數(shù)要返回一個(gè)travel_time結(jié)構(gòu)菩鲜,應(yīng)先聲明一個(gè)travel_time結(jié)構(gòu)
{
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";
}
傳遞結(jié)構(gòu)地址時(shí)园细,函數(shù)不定義為由返回的類型比較方便
#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) //訪問(wèn)結(jié)構(gòu)數(shù)據(jù)的成員用句點(diǎn) .
{?
rect_polar(&zb, &da); //參數(shù)類型是指針,應(yīng)對(duì)結(jié)構(gòu)變量取地址
showploar(&da);
cout << "Next two number(q to quit): ";
}
return 0;
}
void rect_polar(const rect * pxy, polar * pda) //無(wú)返回值接校,用另一個(gè)參數(shù)來(lái)存儲(chǔ)所需結(jié)果
{
const double rad_to_ang = 57.29577951;
pda->dis = sqrt(pxy->x*pxy->x + pxy->y*pxy->y); //訪問(wèn)結(jié)構(gòu)指針的成員用->
pda->angle = atan2(pxy->y, pxy->x)*rad_to_ang;
}
void showploar(const polar * pda)
{
cout <<"distance="<< pda->dis << ", angle=" << pda->angle;
}
14. 聲明string數(shù)組: string list[5];
寫(xiě)入string數(shù)組:for(int i=0;i<5;i++)? getline( cin, list[i] );
15.函數(shù)的遞歸(P239例7.16)
16. 一個(gè)函數(shù)think() ,函數(shù)的名字think即為函數(shù)think()的地址
獲取函數(shù)地址:process(think);? //傳輸think()函數(shù)的地址給process()
? ? ? ? ? ? ? ? ? ? ? ? thought(think());? //傳輸think()函數(shù)的返回值給thought()
聲明函數(shù)指針:double pam( int ); double (*pf) ( int ); pf=pam // pf是一個(gè)指向函數(shù)的指針
double *pf ( int ) ;//表示pf() 返回是指針的函數(shù)
用指針調(diào)用函數(shù): double x=pam(4);? double y=(*pf) (5); 或者 double y=pf (5);
17.在函數(shù)原型中參數(shù)列表const double ar[] 與const double *ar的含義相同猛频。
自動(dòng)類型判斷auto只能用于簡(jiǎn)單的單只初始化,不能用于初始化列表蛛勉。
(1)函數(shù)原型:
const double * f1(const double ar[], int n);
const double * f2(const double? [], int );
const double * f3(const double *, int );
(2)聲明指向函數(shù)的指針:
const double * (*pa) (const double *, int )=f1; //聲明一個(gè)指針鹿寻,指向f1:
const double * (*pb[3]) (const double *, int )={ f1,f2,f3 }; // 聲明一個(gè)指針數(shù)字,指向f1诽凌、f2毡熏、f3,并初始化:
pb是一個(gè)包含3個(gè)指針的 數(shù)組,每個(gè)指針指向一個(gè)函數(shù)皿淋,const double *, int作為參數(shù)招刹,并返回一個(gè)const double *。
auto pa=pb;? //合法
(3)函數(shù)調(diào)用:
const double? *px=pb[0] (av,3); //av,3是參數(shù)? ? 獲取返回的值: double x=*pb[0] (av,3);
const double? *py= (*pb[0]) (av,3);? ? ? ? ? ? ? ? ? ? 獲取返回的值: double y=*(*pb[0] ) (av,3);
18.創(chuàng)建指向整個(gè)指針數(shù)組的指針(P246例7.19)
————————————————
版權(quán)聲明:本文為CSDN博主「lvliang2229」的原創(chuàng)文章窝趣,遵循CC 4.0 BY-SA版權(quán)協(xié)議疯暑,轉(zhuǎn)載請(qǐng)附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/lvliang2017232003/article/details/85759627