C 基礎學習 函式與各種資料結構

2021-10-23 19:26:00 字數 2673 閱讀 2317

int

sum_arr

(int

* arr,

int n)

;

在c++中當且僅當用於函式頭或函式原型的時候,int * arr 和 int arr的含義是一樣的

可以修改陣列內容

void

fmodify

(int arr,

int n)

不可以修改陣列內容

void

nochange

(const

int arr,

int n)

;

①通過傳遞兩個指標完成:函式原型:

int

sumarr

(const

int* begin,

const

int* end)

;

1)定義陣列:

int cookies[size]

=

2)使用:

int sum =

sumarr

(cookies,cookies+size)

;

const讓指標指向乙個常量物件,防止使用指標來修改指標指向位址的字面值

int sum(int (*arr)[4],int size); <*****=> int sum(int arr[4],int size);

第乙個引數都表明,四個指向int的指標組成的陣列,列數是給定的,行數由size指定。

呼叫元素的方式:

arr[r]

[c]=*(

*(arr+r)

+c)

注意形參的定義以及返回值

#include

using

namespace std;

struct polar

;struct rect

;polar rect_to_polar

(rect xyposition)

;int

main()

polar rect_to_polar

(rect xyposition)

#include

using

namespace std;

struct polar

;struct rect

;void

show

(const polar* pda)

;int

main()

void

show

(const polar* pda)

函式名本身就是乙個位址,引用的時候,單純的函式名就是乙個函式的位址,函式名+()表示引用函式的返回值

①必須指定指標指向的函式型別

②舉例:double pam(int a); //函式原型

—> double (*pf)(int a) //定義指標

----> pf = pam 使用(*pf替換函式名)

(3)函式指標作為傳參

use是另外的乙個函式,使用這個函式需要傳兩個值,第乙個整形資料,另乙個是乙個位址,這個位址是函式的位址

①use(int a,double (*pf)(int b)) ----> use(3,pam)

#include

using

namespace std;

intsum

(int a)

;int

use(

int a,

int(

*pf)

(int))

;int

main()

intsum

(int a)

intuse

(int a,

int(

*pf)

(int))

給變數起別名,可以實現跟原名一樣的操作,包括改變變數的字面值。

本質:在c++內部實現的是乙個指標常量

①typename & 別名 = 變數名

宣告的時候必須初始化

初始化之後不可以改變

void

swap

(int

& a,

int& b)

不要返回區域性變數的引用 即放在棧區的變數

②函式的呼叫可以作為左值使用

#include

using

namespace std;

int&

test()

;int

main()

int&

test()

函式預設值

(1)使用者輸入的引數優先順序高於預設引數

(2)某個形參有預設值,則後面的每乙個形參都要有預設值,否則會報錯

(3)函式宣告和函式實現兩個地方只能有乙個地方有預設值,不能同時設定預設值,一樣的也不行

活用各種資料結構

吊車由n條不同長度的線段組成,每條線段首尾相接。初始狀態 每條線段垂直與x軸。每次操作改變第s條和 s 1 條的逆時針角度為a,詢問每次操作後第n段末尾的座標。將每條線段都當成向量,實際上每次詢問的結果是向量和 每次改變第s段和第 s 1 段的相對角度,實際上是改變了從第 s 1 段至第n段的各節點...

Redis入門 各種資料結構

string 型別 儲存形式 以 key value 形式存在 常用命令 set get del incr incr setnx list 型別 儲存形式 以 key 集合 形式存在 常用命令 rpush lpush llen lrange lpop rpop set 型別 儲存形式 以 key 集...

redis 各種資料結構的encoding實現

redis 各種資料結構的encoding實現 redis type命令實際返回的就是當前鍵的資料結構型別,它們分別是 string 字串 hash 雜湊 list 列表 set 集合 zset 有序集合 但這些只是redis對外的資料結構。我們可以通過object encoding命令查詢內部編碼...