【漫漫轉(zhuǎn)碼路】Day31C++day04-創(chuàng)新互聯(lián)

指針 指針指向數(shù)字

存儲其他變量地址的變量,稱為指針;

站在用戶的角度思考問題,與客戶深入溝通,找到鄄城網(wǎng)站設(shè)計與鄄城網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗,讓設(shè)計與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:成都做網(wǎng)站、成都網(wǎng)站建設(shè)、成都外貿(mào)網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣、域名申請、虛擬空間、企業(yè)郵箱。業(yè)務(wù)覆蓋鄄城地區(qū)。
int a = 25;
int *b = &a;  // 此時會報錯,“int類型的值不能用于初始化int類型的實體”
// &a表示指向a的內(nèi)存地址;

存儲內(nèi)存地址的叫指針,不能使用0以外的整型對指針賦值,因此不能指向a,只能指向a的地址
也就是&a(表示a的內(nèi)存地址)

//例如
#include#include#includeusing namespace std;
int main()
{int a = 5;
	int *b = &a;
	cout<< b<< "\n";  // b得到的是a的內(nèi)存地址
	cout<< *b<< "\n";  // *b得到的是內(nèi)存地址存儲的值
	a = 10;  //修改a
	cout<< b<< "\n";  // b獲取的內(nèi)存地址不變
	cout<< *b<< "\n";  // 但是內(nèi)存地址對應(yīng)的值變了
	*b = 8;  // 通過修改內(nèi)存地址對應(yīng)的數(shù)據(jù)
	cout<< a<< "\n";  // 也就是直接修改a
}
//顯示
0113FACC
5
0113FACC
10
8
指針指向數(shù)組

指針指向數(shù)組不需要加&來指向他的地址

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

直接指向內(nèi)存中首地址,也就是索引為0的地址

//例如
#include#include#includeusing namespace std;
int main()
{	int c[5] = {1, 2, 3, 4, 5 };
	int* d = c;  // 直接指向數(shù)組開始的內(nèi)存
	cout<< c[1]<< "  "<< *(d + 1);
// *(d + 1)是指針偏移,表示向右移動一位,原來在開始的位置,也就是索引為0的位置,偏移之后就到了索引為1的位置,所以能輸出2
}
//顯示
2 2

*(d + 1)表示是往右偏移一個單位的長度
指針不允許越界訪問,就是不能超出索引

//例如
#include#include#includeusing namespace std;
int main()
{int c[5] = {1, 2, 3, 4, 5 };
	int* d = c;
	c[1] = 5;  //修改之后,兩個都發(fā)生變化
	cout<< c[1]<< "  "<< *(d + 1);
}
//顯示
5  5
空指針
int *a = nullptr;

空指針不指向任何地址,也不能指向任何地址,不能修改,只是一個空指針
空指針nullptr在if語句中會被當成False
NULL 或 0 也會被認為是空指針

二維數(shù)組的指針

二維及多維數(shù)組的指針需要在數(shù)組前面加強制類型轉(zhuǎn)換(int *)

//例如
#include#include#includeusing namespace std;
int main()
{int c[3][3] = {{1, 2, 3},
		{4, 5, 6},
		{7, 8, 9}
	};
	int* d = (int *)c;  // 強制類型轉(zhuǎn)換
	cout<< *(d + 2)<< "  "<< *(d + 3)<< "\n";  // (d + 2)是3,(d + 3)是4
	cout<< *c[1]<< "  "<< *c[1]+1;  // c是二維數(shù)組,因此一開始指針默認在數(shù)組的開始,也就是c[0][0]處,*c[1]意味著指針懸停到了第一行的開始,也就是c[1][0]處,也就是4,因此*c[1]+1表示在此基礎(chǔ)上右偏移一個,也就是5;
}
//顯示
3  4
4  5

二維數(shù)組在內(nèi)存中是以行來保存的

//例如
#include#include#includeusing namespace std;
int main()
{int a[5] = {1, 2, 3, 4, 5 };
	// int* b = a;  
	int* b = &a[0];  // 與上式寫法等效,也就是指針指向數(shù)組最開始的內(nèi)存地址
	cout<< *(b + 3);
}
//顯示
4

指針可以要求指向第幾個地址;

//例如
#include#include#includeusing namespace std;
int main()
{int a[5] = {1, 2, 3, 4, 5 };
	int* b = &a[2];  // 此時指針指向索引為2的元素
	cout<< *(b + 1);  // 再次右偏
}
//顯示
4 // 索引2右偏1,得到索引3
//例如
#include#include#includeusing namespace std;
int main()
{int a[5] = {1, 2, 3, 4, 5 };
	int* b = &a[2];
	*(b - 1) = 999;
	for (int i = 0; i< 5; i++)
	{cout<< a[i]<< " ";
	}
}
//顯示
1 999 3 4 5  //(b - 1)是索引1,左移
引用

引用的數(shù)據(jù)成為引用變量,b就是引用變量,引用支持多個引用變量引用一個數(shù)據(jù),以及連續(xù)引用,
修改其中哪一個,其他都將發(fā)生變化

//例如
#include#include#includeusing namespace std;
int main()
{int a = 33;
	int &b = a;
	cout<< b<< "\n";
	b = 55;
	cout<
函數(shù)

函數(shù)要先聲明再使用,函數(shù)的聲明需要在函數(shù)主體int main前面;

type name (argument1, argument2...) statement
type 是返回類型
name 是定義的函數(shù)名
argument是上傳的參數(shù)
statement是語句

加法函數(shù)

//例如
#include#include#includeusing namespace std;
int func(int a, int b)
{int c = a + b;
	return c;
}
int main()
{int d = func(1, 3);  // 調(diào)用函數(shù)
	cout<< d;
}
//顯示
4
入口參數(shù)

入口參數(shù)的隱式轉(zhuǎn)換
入口參數(shù)做值拷貝,就是只有值一樣,函數(shù)內(nèi)部會對拷貝得到的數(shù)據(jù)進行執(zhí)行運算,不會對原數(shù)據(jù)有任何影響

//例如
#include#include#includeusing namespace std;
int func(int a)
{a = 10;  // 對a進行復(fù)制,此時a是值拷貝得到的a,
	return 0;
}
int main()
{int a = 3;
	int d = func(a); // 將a= 3 傳進去,會對a做值拷貝,
	cout<< d<< "\n"; // d是返回值,是return的值
	cout<< a;//調(diào)用完函數(shù)之后應(yīng)該是a = 10,但那是函數(shù)內(nèi)部,在函數(shù)外部的a沒有改變,還是3
}
//顯示
0
3
函數(shù)返回兩個數(shù)

函數(shù)只能有一個數(shù)值,如果要返回兩個數(shù)值,可以返回引用,或者內(nèi)存地址

用引用
//例如
#include#include#includeusing namespace std;
float func(float r, float &D)  //此處引用D
{float S = 3.14 * r * r;
	D = 2 * 3.14 * r;
	return S;
}
int main()
{int a = 10;
	float d1;
	float d = func(a,d1);  // 相當于有一個&D = d1,此時D和d1是引用和被引用關(guān)系,一方被修改,則兩方都被修改,而函數(shù)體中的D = 2 * 3.14 * r;就是修改D,那么d1也會被修改,那么就可以從函數(shù)體中得到另一個參數(shù)了
	cout<< d<< "\n";
	cout<< d1;
指針指向內(nèi)存地址
//例如
#include#include#includeusing namespace std;
float func(float r, float *D)
{float S = 3.14 * r * r;
	*D = 2 * 3.14 * r;
	return S;
}
int main()
{int a = 10;
	float d1;
	float d = func(a, &d1);  相當于有一個*D = &d1,此時D直接指向d1的內(nèi)存地址,一方被修改,則兩方都被修改,而函數(shù)體中的D = 2 * 3.14 * r;就是修改D,那么d1也會被修改,那么就可以從函數(shù)體中得到另一個參數(shù)了
	cout<< d<< "\n";
	cout<< d1;
}
a和b交換值

一、利用引用

//例如
#include#include#includeusing namespace std;
void func(float &a, float &b)
{float temp = a;
	a = b;
	b = temp;
}
int main()
{float a = 2;
	float b = 3;
	func(a, b);
	cout<< a<< "\n";
	cout<< b;
}

二、利用指針指向地址

//例如
#include#include#includeusing namespace std;
void func(float *a, float *b)
{float temp = *a;
	*a = *b;
	*b = temp;
}
int main()
{float a = 2;
	float b = 3;
	func(&a, &b);
	cout<< a<< "\n";
	cout<< b;
}
空函數(shù)
int func () // 不上傳參數(shù)的函數(shù)是可以存在的,一般用功能性參數(shù)
{return 1.0;
}
運算符的重載

可以存在函數(shù)名相同的函數(shù),但是上傳參數(shù)的類型或上傳參數(shù)的個數(shù)不能一樣
在c++中,輸入1.5其實是double類型,1.5f才是float類型
double雙精度浮點數(shù)

//例如
#include#include#includeusing namespace std;
void func(int a)
{cout<< "運行了整型func";
}
void func(float a)  //兩個函數(shù)名一樣,但是上傳參數(shù)的類型不一樣
{cout<< "運行了浮點型func";
}
int main()
{float a = 2;
	int b = 3;
	func(a);  //a是浮點型,調(diào)用浮點型func
	func(b);  //b是整型,調(diào)用整型func
	double c = 4;
	func(4);  // 會報錯,因為沒有定義雙精度浮點型的func,不知道選哪一個,但是如果去掉一個func,只留一個func,就會隱式轉(zhuǎn)換**
}
//顯示
運行了浮點型func運行了整型func  //沒有加換行

如果有兩個func,不知道選哪一個,就會報錯,但是如果去掉一個func,只留一個func,就會隱式轉(zhuǎn)換
函數(shù)可以在int main前面聲明,然后在int main后面寫函數(shù),也不會報錯

缺省參數(shù)(默認參數(shù))

函數(shù)里面可以有默認參數(shù)

//例如
#include#include#includeusing namespace std;
int func(int a,int b=5)
{int c = a + b;
	cout<< c<< "\n";
	return c;
}

int main()
{float a = 2;
	int b = 3;
	func(a);  //默認b=5
	func(a,b); // 上傳參數(shù)的時候優(yōu)先使用上傳的參數(shù)
}
//顯示
7
5
遞歸

遞歸深度是跟程序內(nèi)存占據(jù)大小有關(guān)
遞歸實現(xiàn)n的階乘

//例如
#include#include#includeusing namespace std;
int func(int n)
{if (n<= 1)
	{return 1;
	}
	return n * func(n-1);
}

int main()
{int res = func(3);
	cout<< res;
}
//顯示
6

遞歸實現(xiàn)x*x+sin(x)-2求解

//例如
#include#include#includeusing namespace std;
// x*x+sin(x)-2
float func(float left, float right)
{if ((abs(left - right))< 0.00001)
	{return left;
	}
	float r_left = left * left + sin(left) - 2;
	float r_right = right * right + sin(right) - 2;
	float mid = (left + right) / 2;
	float r_mid = mid * mid + sin(mid) - 2;
	if (r_left * r_mid< 0)
	{return func(left, mid);
	}
	else
	{return func(mid, right);
	}
}

int main()
{float res = func(0, 10);
	cout<< res;
}
//顯示
1.06154
void函數(shù)

無返回值的函數(shù)定義為void

靜態(tài)函數(shù)

static,靜態(tài)函數(shù),在函數(shù)內(nèi)部存在的變量,函數(shù)內(nèi)部共享

//例如
#include#include#includeusing namespace std;
void func()
{static int a = 0;  //靜態(tài)函數(shù)的初始化只會執(zhí)行一次
	a++;
	cout<< a<< "\n";
}

int main()
{func();
	func();
	func();
	func();
	func();
}
//顯示
1
2
3
4
5

兩個函數(shù)之間的靜態(tài)變量互不關(guān)聯(lián),沒有影響

//例如
#include#include#includeusing namespace std;
void F2()
{static int b = 0;
	b++;
	cout<< "F2的靜態(tài)函數(shù)"<< b<< "\n";
}
void F1()
{static int a = 0;
	a++;
	cout<< "F1的靜態(tài)函數(shù)"<< a<< "\n";
	F2();
}


int main()
{F1();
	F1();
	F2();
}
//顯示
F1的靜態(tài)函數(shù)1  // F1和F2的靜態(tài)函數(shù)的計數(shù)互不影響,沒有關(guān)聯(lián)
F2的靜態(tài)函數(shù)1
F1的靜態(tài)函數(shù)2
F2的靜態(tài)函數(shù)2
F2的靜態(tài)函數(shù)3

你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機房具備T級流量清洗系統(tǒng)配攻擊溯源,準確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧

當前文章:【漫漫轉(zhuǎn)碼路】Day31C++day04-創(chuàng)新互聯(lián)
標題鏈接:http://muchs.cn/article14/dppege.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務(wù)、網(wǎng)站制作、小程序開發(fā)、微信公眾號用戶體驗、Google

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)