C++基礎
VS快捷鍵
Ctrl +或- 跳轉到上次滑鼠焦點位置
Ctrl K F 按住Ctrl 然后按K 然后按F
Ctrl J 代碼提示
變數
宣告方式:資料型別 變數名 = 變數初始值
#include <iostream>
using namespace std;
int main()
{
int a = 1;
char b = 'b';
}
常量
#define 宏常量
宣告方式:#define 常量名 常量值 通常在檔案上方定義
#include <iostream>
using namespace std;
#define day 7
int main()
{
cout << "一周有" << day <<"天" << endl;
}
const修飾的變數
宣告方式:const 資料型別 變數名 = 變數值
#include <iostream>
using namespace std;
int main()
{
const int day = 7;
cout << "一周有" << day <<"天" << endl;
}
命名規則
- 識別符號不能是關鍵字
- 識別符號只能有字母,數字,下劃線
- 第一個字符必須為字母或下劃線
- 區分大小寫
資料型別
整型
- short 2位元組
- int 4位元組
- long windows 4位元組 Linux32位 4位元組 Linux64位 8位元組
- long long 8位元組
sizeof關鍵字
- 用來獲取資料型別所占用記憶體大小
- 語法: sizeof( 資料型別 / 變數 )
#include <iostream>
using namespace std;
int main()
{
int a = 1;
long b = 1;
long long c = 1;
cout << "int型別占用:" << sizeof(int) << endl;
cout << "int變數占用:" << sizeof(a) << endl;
cout << "long型別占用:" << sizeof(long) << endl;
cout << "long變數占用:" << sizeof(b) << endl;
cout << "long long型別占用:" << sizeof(long long) << endl;
cout << "long long變數占用:" << sizeof(c) << endl;
}
輸出結果(我使用的是windows 所以long型別也是4位元組大小):
int型別占用:4
int變數占用:4
long型別占用:4
long變數占用:4
long long型別占用:8
long long變數占用:8
浮點型
- double 8位元組 有效數字范圍15-16位
- float 4位元組 有效數字范圍7位
需要注意的一點是:當我們宣告一個float型別的帶小數的變數時,編輯器會默認認為是double型別,準確指出型別需要在變數值最后加F
#include <iostream>
using namespace std;
int main()
{
float a = 1.1F;
float b = 1.1;
}
上面兩種都可
字符型
語法: char c='a'
- 字符型別只能使用單引號,而不是雙引號
- 只能是單個字符,不能是字串
- c/c++中字符型別只占用
1位元組 - 字符型別并不是把字符本身放到記憶體中存盤,而是轉換為對應的ASCll碼存放
#include <iostream>
using namespace std;
int main()
{
char a = 'h';
char b = 'e';
cout << "變數a:" << a << endl;
cout << "變數b:" << b << endl;
}
輸出char型別變數的ascll碼
強轉為int型別輸出即可
#include <iostream>
using namespace std;
int main()
{
char a = 'h';
char b = 'e';
cout << "變數a:" << (int)a << endl;
cout << "變數b:" << (int)b << endl;
}
轉義字符
就寫幾個常用的
- \n 換號
- \t 一個table的位置
- \\ 代表一個\
字串型別
C風格字串
宣告方式: char 變數名[] = "字串值"
#include <iostream>
using namespace std;
int main()
{
char s[] = "hello world";
cout << s << endl;
}
C++風格字串
宣告方式:string 變數名="字串值"
如果使用不了string 需要匯入頭檔案
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s = "hello world";
cout << s << endl;
}
Bool型別
表示真和假,只有兩個值
- false 0
- true 1
bool型別只占用一個位元組大小
#include <iostream>
#include <string>
using namespace std;
int main()
{
bool b = true;
cout << b << endl;
}
資料的輸入
用于獲取鍵盤的輸入
關鍵詞:cin 語法: cin>>變數
#include <iostream>
#include <string>
using namespace std;
int main()
{
int a = 1;
cin >> a;
cout << a << endl;
}
運算子
- 算數運算子 處理四則運算
- 賦值運算子 將運算式賦值給變數
- 比較運算子 運算式的比較并回傳一個bool值
- 邏輯運算子 用于根據運算式的值回傳true或false
算數運算子
| 運算子 | 術語 | 示例 | 結果 |
|---|---|---|---|
| + | 正號 | +3 | 3 |
| - | 負號 | -3 | -3 |
| + | 加號 | 10+5 | 15 |
| - | 減號 | 10-5 | 5 |
| * | 乘號 | 10*5 | 50 |
| / | 除號 | 10/5 | 2 |
| % | 取模(取余) | 10%3 | 1 |
| ++ | 前置自增 | a=2;b=++a; | a=3;b=3; |
| ++ | 后置自增 | a=2;b=a++; | a=3;b=2; |
| -- | 前置自減 | a=2;b=--a; | a=1;b=1; |
| -- | 后置自減 | a=2;b=a--; | a=1,b=2 |
tips: 兩數相除,除數不能為0
兩數取余,第二個數也不能為0;
兩個小數是不能做取余操作
int main()
{
int a = 2;
int b = a++;
//其他操作也基本類似,就不粘代碼了
cout << a << endl;
cout << b << endl;
}
賦值運算子
| 運算子 | 術語 | 示例 | 結果 |
|---|---|---|---|
| = | 賦值 | a=2; | a=2; |
| += | 加等于 | a=2; a+=1; | a=3; |
| -= | 減等于 | a=2;a-=1; | a=1; |
| *= | 乘等于 | a=2;a*=2; | a=4; |
| /= | 除等于 | a=4;a/=2; | a=2; |
| %= | 模等于 | a=3;a%=2; | a=1; |
#include <iostream>
#include <string>
using namespace std;
int main()
{
int a = 1;
a += 1;
// a = a + 1; 等同于這段代碼 其他操作類似
cout << a << endl;
}
比較運算子
| 運算子 | 術語 | 示例 | 結果 |
|---|---|---|---|
| == | 相等于 | 1==1 | 1 (true) |
| != | 不等于 | 1!=1 | 0 (false) |
| < | 小于 | 1<1 | 0 (false) |
| <= | 小于等于 | 1<=1 | 1 (true) |
| > | 大于 | 1>1 | 0 (false) |
| >= | 大于等于 | 1>=1 | 1 (true) |
#include <iostream>
#include <string>
using namespace std;
int main()
{
cout << (1<=1) << endl;
}
邏輯運算子
| 運算子 | 術語 | 示例 | 結果 |
|---|---|---|---|
| ! | 非(取反) | !a | 如果a為假,則!a為真 反之亦然 |
| && | 與 (并且) | a&&b | 如果ab都為真,則結果為真,其中一個為假,則整體為假 |
| || | 或 (或者) | a||b | 只要其中一個為真,則結果為真,都為假則結果為假 |
#include <iostream>
#include <string>
using namespace std;
int main()
{
int a = 10;
cout << !a << endl;
/*
結果為0 因為邏輯運算子會把變數當成bool型別來操作,bool中只有0和非0
那么10作為非0的數值被當成true,取反為false, 而false的int型別數值為0
所以結果為0
*/
cout << !!a << endl;
/*
結果為1 原因如上,當第一個!走完后結果已經為0了,而bool值只有0和非0
非零默認值是1,所以兩次取反后結果為1
*/
//加幾個!沒有限制,可以無限加
}
#include <iostream>
#include <string>
using namespace std;
int main()
{
int a = 10;
int b = 0;
cout << (a && b) << endl;
/*
結果為0 (false)
*/
}
#include <iostream>
#include <string>
using namespace std;
int main()
{
int a = 10;
int b = 0;
cout << (a|| b) << endl;
/*
結果為1 (true)
*/
}
程式流程結構
選擇結構
if
#include <iostream>
#include <string>
using namespace std;
int main(){
int a = 10;
int b = 0;
if (a) {
cout << "a" << endl;
}
else if (b){
cout <<"b" << endl;
}
else {
cout << "else" << endl;
}
}
三目運算子
語法:條件?為true回傳的:否則回傳的
例如a=1;b=2;
a>b?a:b
a大于b嗎?是的回傳a,否則回傳b
#include <iostream>
#include <string>
using namespace std;
int main() {
int a = 10;
int b = 0;
int c = a > b ? a : b;
cout << c << endl;
/*
結果10
*/
}
#include <iostream>
#include <string>
using namespace std;
int main() {
int a = 10;
int b = 0;
//回傳變數并賦值
( a > b ? a : b)=50;
cout << a << endl;
cout << b << endl;
/*
結果a=50
b=0;
*/
}
不僅可以回傳變數,也可以在三目中進行賦值操作等
#include <iostream>
#include <string>
using namespace std;
int main() {
int a = 10;
int b = 0;
int c;
a > b ? c = 10 : c = 20;
cout << c << endl;
//結果c=10;
}
switch
#include <iostream>
#include <string>
using namespace std;
int main() {
char a = 's';
switch (a)
{
case 's':
cout << "s" << endl;
break;
case 'h':
cout << "h" << endl;
break;
default:
cout << "default" << endl;
break;
}
//結果 s
}
#include <iostream>
#include <string>
using namespace std;
int main() {
int a = 3;
switch (a)
{
case 1:
cout << "1" << endl;
break;
case 2:
cout << "2" << endl;
break;
default:
cout << "default" << endl;
break;
}
//結果 default
}
tips : 如果一個case沒有break,則會繼續執行下面的case直到結束 default也會執行
回圈結構
while
語法:while(條件){回圈陳述句}
#include <iostream>
#include <string>
using namespace std;
int main() {
int a = 1;
while (a < 10) {
cout << ++a << endl;
}
//一直回圈到10結束
}
do..while
語法: do{回圈陳述句} while(條件)
#include <iostream>
#include <string>
using namespace std;
int main() {
bool eat = false;
do {
cout << "吃個漢堡,還要吃嗎?"<< endl;
cin >> eat;
} while (eat);
/*
結果:
吃個漢堡,還要吃嗎?
1
吃個漢堡,還要吃嗎?
1
吃個漢堡,還要吃嗎?
0
程式結束
*/
}
for
語法:for(起始變數;回圈條件;回圈結束執行代碼){回圈陳述句}
#include <iostream>
#include <string>
using namespace std;
int main() {
char my_name[] = "jame";
for (int i = 0; i < sizeof(my_name); i++)
{
cout << my_name[i] << endl;
}
/*
sizeof可以獲取到占用的記憶體大小,就是char的個數 一個char占一位元組 那么sizeof(my_name)結果就是4
每次回圈輸出具體下標的內容就可以完成遍歷了
結果:
j
a
m
e
*/
}
小練習:列印乘法表
#include <iostream>
#include <string>
using namespace std;
int main() {
for (int i = 1; i <= 9; i++)
{
for (int j = 1; j <= i; j++)
{
cout << i << "*" << j << "=" << (i*j) << " ";
}
cout << "\n";
}
}
1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
4*1=4 4*2=8 4*3=12 4*4=16
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
跳轉陳述句
語法: break
使用的時機:
- 在switch中,終止case并跳出switch
- 回圈中跳出當前回圈
上面的乘法表中使用
#include <iostream>
#include <string>
using namespace std;
int main() {
for (int i = 1; i <= 9; i++)
{
for (int j = 1; j <= i; j++)
{
if (j == i)
{
break;
}
cout << i << "*" << j << "=" << (i*j) << " ";
}
cout << "\n";
}
}
2*1=2
3*1=3 3*2=6
4*1=4 4*2=8 4*3=12
5*1=5 5*2=10 5*3=15 5*4=20
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72
當i==j的時候直接跳出回圈了,所以看不到1乘1,2乘2這樣類似的輸出
contiune
當在回圈中出現contiune時,直接進行下次回圈
#include <iostream>
#include <string>
using namespace std;
int main() {
for (int i = 1; i <= 9; i++)
{
if (i % 2 == 0)
{
continue;
}
cout << i;
}
}
//結果133579
goto
無條件跳轉
語法:goto 標記
如果標記的位置存在,則執行到goto時會直接跳轉到標記的位置
#include <iostream>
#include <string>
using namespace std;
int main() {
for (int i = 1; i <= 9; i++)
{
if (i == 8)
{
goto END;
}
cout << i;
}
END:
cout << "結束啦";
}
不光是往下面跳,還能往上跳
#include <iostream>
#include <string>
using namespace std;
int main() {
int b = 0;
RESTART:
if (b > 0) {
cout << "b!" << endl;
}
for (int i = 1; i <= 9; i++)
{
if (i == 8 && b == 0)
{
b = 10;
goto RESTART;
}
cout << i ;
}
}
/*
結果
1234567b!
123456789
*/
使用goto向上面跳時一定要注意結束的判斷,避免死回圈
陣列
特點1:同個陣列內的每個元素都相同型別的
特點2:陣列是記憶體中一塊連續記憶體地址組成的
定義陣列
int main() {
int a[10];
int b[10] = { 1,2,3 }; //如果標記長度為10,但是只初始化3個值,那么剩下的默認值都是0
int c[] = { 1,2,3 };
}
三種定義陣列的方式
訪問陣列
int main() {
int a[10];
a[1] = 1;
a[2] = 100;
}
陣列名用途
- 可以獲取整個陣列在記憶體中的長度
- 可以獲取記憶體中陣列的首地址
#include <iostream>
#include <string>
using namespace std;
int main() {
int a[10] = { 1,2,3 };
cout << "陣列占用總大小" << sizeof(a) << endl;
cout << "每個元素占用大小" << sizeof(a[0]) << endl;
cout << "陣列中有多少個元素" << sizeof(a)/sizeof(a[0]) << endl;
}
陣列占用總大小40
每個元素占用大小4
陣列中有多少個元素10
tips: 陣列中有多少個元素 這里使用a[0] 而不是其他a[1],a[2]...的原因 首先陣列的特性1,每個元素都是相同型別的,也就是所有的元素大小都是一樣的,使用那個都可以
那么也可以使用a[1],a[2]... 但是需要注意的是下標不能超過定義的最大長度
int main() {
int a[10] = { 1,2,3 };
cout << "陣列首地址"<< a << endl;
cout << "陣列第一個元素的首地址"<< &a[0] << endl;
}
tips: &在變數前是獲取元素的地址
二維陣列
定義:
#include <iostream>
#include <string>
using namespace std;
int main() {
int a[3][3]; //宣告一個三行三列的陣列
int b[3][3] = { {1,2,3},{4,5,6},{7,8,9} }; //宣告三行三列,同時賦初始值
int c[3][3] = { 1,2,3,4,5,6,7,8,9 }; //宣告三行三列 同時全部賦值 三個為一行
int d[][3] = { 1,2,3,4,5,6 }; //宣告一個兩行三列 同時賦值
}
陣列名稱
#include <iostream>
#include <string>
using namespace std;
int main() {
int b[3][3] = { {1,2,3},{4,5,6},{7,8,9} }; //宣告三行三列,同時賦初始值
cout << "二維陣列首地址:" << b << endl;
cout << "二維陣列一行大小:" << sizeof(b[0]) << endl;
cout << "二維陣列元素大小:" << sizeof(b[0][0]) << endl;
}
二維陣列遍歷
#include <iostream>
#include <string>
using namespace std;
int main() {
int b[3][3] = { {1,2,3},{4,5,6},{7,8,9} }; //宣告三行三列,同時賦初始值
for (int i = 0; i < (sizeof(b)/sizeof(b[0])); i++)
{
for (int j = 0; j < (sizeof(b[0])/sizeof(b[0][0])); j++)
{
cout << b[i][j];
}
}
}
函式(方法)
一個函式包含以下內容:
- 回傳值型別
- 函式名稱
- 引數串列
- 函式體
- return
//回傳值型別和函式名稱,引數為空
int main() {
//函式體
cout << "hello world";
//return值
return 0;
}
函式的呼叫
int sum(int number1, int number2) {
return number1 + number2;
}
int main() {
cout << sum(1,2);
return 0;
}
tips: C++和JAVA不一樣,被呼叫的函式必須在呼叫函式的方法前宣告
值傳遞
值傳遞是指當實參傳遞給形參后,當形參發生改變,實參并不會發生變化
int test(int number1) {
number1 += 1;
return 0 ;
}
int main() {
int a = 1;
test(a);
cout << a;
return 0;
}
當呼叫完test函式后a的值輸出還是1,或者我們輸出number1和a的地址,也會發現不同,說明test的引數只是值傳遞
int test(int number1)
{
number1 += 1;
cout << &number1 << endl;
return 0 ;
}
int main()
{
int a = 1;
test(a);
cout << &a<<endl;
return 0;
}
/*
000000FF976FF550
000000FF976FF574
*/
函式常見樣式
-
有參有返
int sum(int a,int b) { return a+b; } -
有參無返
void print_number(int number) { cout << number; } -
無參有返
int get_one() { return 1; } -
無參無返
void print_hello(){ cout << "hello"; }
函式的宣告
在函式的開始篇有個tips 說使用的被呼叫的函式必須宣告在呼叫的函式前,而函式宣告就可以讓被呼叫的函式宣告在呼叫的函式之后
#include <iostream>
#include <string>
using namespace std;
int main() {
int a = 1;
print(a);
return 0;
}
void print(int a) {
cout << a;
}
我們這么寫編譯是沒有問題,但是當運行起來后就會報錯:print找不到識別符號
調換個位置就可以正常運行
void print(int a) {
cout << a;
}
int main() {
int a = 1;
print(a);
return 0;
}
而函式的宣告就是提前宣告我有這個函式
語法:回傳型別 函式名(引數); 一個函式可以被宣告多次,但是只能定義一次
#include <iostream>
#include <string>
using namespace std;
void print(int a); //提前宣告存在print函式
int main() {
int a = 1;
print(a);
return 0;
}
void print(int a) {
cout << a;
}
函式分檔案撰寫
將相同功能的函式單獨放在一個檔案中,使結構更加清晰
- 創建后綴為
.h的頭檔案 - 創建后綴為
.cpp的源檔案 - 在頭檔案中寫函式宣告
- 在源檔案中寫函式實作
myHead.h
#include <iostream>
using namespace std;
void print(int a);
myHead.cpp
#include "myHead.h";
void print(int a) {
cout << a;
}
主檔案
#include "myHead.h";
int main() {
int a = 1;
print(a);
return 0;
}
我們在.h頭檔案中引入了iostream 也使用了std 那么只要匯入myHead.h檔案的都可以使用
咋說呢,有點像java的介面?哈哈哈
指標
指標的作用:可以通過指標直接訪問記憶體
- 記憶體編號從0開始,一般為16進制數字表示
- 可以使用
指標變數保存地址
指標的定義和使用
語法:資料型別* 變數名
int main() {
int a = 1;
int* a_pointer = &a;//將a的地址存放到指標中
*a_pointer = 100;//將100賦值給指標指向的地址中 指標前加*代表解參考 找到指標指向記憶體中的資料
cout << a << endl;
cout << "a的地址" << &a<<endl;
cout << "a指標的資料"<<a_pointer << endl;
}
指標占用的記憶體
int main() {
cout << sizeof(int *) << endl;
cout << sizeof(short *) << endl;
cout << sizeof(float *) << endl;
cout << sizeof(double *) << endl;
cout << sizeof(long *) << endl;
cout << sizeof(long long *) << endl;
}
32位中都是4位元組 64位中都是8位元組
空指標和野指標
空指標:指標變數指向記憶體中編號為0的空間
用途: 初始化指標
注意: 空指標的記憶體是不可訪問的
int main() {
int* a = NULL;
cout << *a << endl;
//*a = 100; 或者這段代碼
}
//都會報一個訪問權限的例外
野指標:指標變數指向非法的記憶體空間
int main() {
int* a = (int*)0x10000; //(int * 強制轉換為指標型別)
*a = 100;
}
//也會報一個訪問權限的例外
tips: 空指標和野指標都不是我們申請的空間,因此不要訪問
const修飾指標
const有三種情況
- 修飾指標:常量指標
- 修飾常量:指標常量
- 即修飾指標,又修飾常量
常量指標
int main() {
int a = 10;
int b = 20;
/*
* 常量指標
* 指標指向的值不可修改,但是可以修改指向
*/
const int* p = &a;
//*p = 10; 修改指向的值 錯誤
p = &b;//修改指向 可以
}
指標常量
int main() {
int a = 10;
int b = 20;
/*
* 指標常量
* 指標指向的值不可修改,但是可以修改指向
*/
int* const p = &a;
*p = 10; //修改指向的值 可以
//p = &b;//修改指向 錯誤
}
即修飾指標,又修飾常量
int main() {
int a = 10;
int b = 20;
/*
* 即修飾指標,也修飾常量
* 指標指向的值不可修改,但是可以修改指向
*/
const int* const p = &a;
//*p = 10; //修改指向的值 錯誤
//p = &b;//修改指向 錯誤
}
指標和陣列
利用指標訪陣列中的元素
int main() {
int a[5] = { 1,2,3,4,5 };
int* p = a;
cout << *p;
p++;
cout << *p;
}
tips: 指標型別++ 加的是sizeof(指標型別) 的值, int加4位元組正好是下個元素的地址 然后*p解參考取值也就是下個元素的值
指標和函式
利用指標作為函式的引數,從而修改實參的值,也就是常說的值傳遞和參考傳遞,而當使用指標作為引數傳遞時,就是參考傳遞
void add(int* num1) {
(*num1)++;
}
int main() {
int a = 10;
add(&a);
cout << a;
}
當執行完后a的值為11
tips: 括號標明是先去獲取指標指向的值,然后自增1,如果不加可能以為是指標值++然后取值
指標陣列和函式
小練習 冒泡
int* arr接收陣列的地址,可以當做陣列使用
void golugolu(int* arr ,int lenght)
{
for (int i = 0; i < lenght; i++)
{
for (int j = 0; j < lenght - i- 1; j++)
{
if (arr[j] > arr[j + 1])
{
int temp = arr[j];
arr[j]=arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main() {
int a[5] = { 2,3,1,4,5 };
golugolu(a,5);
for (int i = 0; i < 5; i++)
{
cout << a[i];
}
}
結構體
結構體屬于用戶自定義的資料型別,允許用戶存盤不同的資料型別
語法: struct 結構體名 {結構體成員串列};
tpis:
注意! 結構體結束}后面需要加一個;結尾
通過結構體創建變數的方式有三種:
- struct 結構體名 變數名
- struct 結構體名 變數名={成員1值,成員2值}
- 定義結構體時同時設定變數
方式1
//定義結構體
struct Student
{
//宣告結構體中的成員
string name;
int score;
int age;
};
int main()
{
struct Student student; //struct可以省略
student.age = 10;
student.name = "張三";
student.score = 90;
cout << student.age << endl;
}
方式2
#include <iostream>
#include <string> //如果string型別報錯則匯入該頭檔案
using namespace std;
//定義結構體
struct Student
{
//宣告結構體中的成員
string name;
int score;
int age;
};
int main()
{
struct Student student = { "張三",90,18 }; //struct可以省略
cout << student.name << endl;
}
類似于有參構造,每個變數的順序就是在結構體中宣告的順序
方式3
//定義結構體
struct Student
{
//宣告結構體中的成員
string name;
int score;
int age;
} oldStudnet; //標明一個結構體
int main()
{
//可以使用上面兩種來進行填充資料
oldStudnet = { "we",1,1 };
oldStudnet.age = 199;
cout << oldStudnet.name << endl;
cout << oldStudnet.age << endl;
}
結構體陣列
語法:struct 結構體名 陣列名[個數]={{},{},{},...{}
//定義結構體
struct Student
{
//宣告結構體中的成員
string name;
int score;
int age;
};
int main()
{
struct Student arr[3] =
{
{"張三",1,1},
{"李四",2,2},
{"王五",3,3}
};
//整個陣列長度除以每個元素長度獲取到多少個元素
for (int i = 0; i < sizeof(arr) / sizeof(arr[0]); i++)
{
//在回圈中就可以將arr[i]當成具體的struct
arr[i].age = 10;
cout << arr[i].name;
}
}
結構體指標
語法: 可以通過-> 來訪問結構體中的屬性
//定義結構體
struct Student
{
//宣告結構體中的成員
string name;
int score;
int age;
};
int main()
{
//創建結構體變數
struct Student studnet = { "張三",1,1 };
//創建指標變數,同時指向student變數
struct Student* p = &studnet;
//使用-> 運算子操作變數age
p->age = 100;
cout << p->age;
}
結構體嵌套結構體
struct Dog {
string name;
int age;
};
//定義結構體
struct Student
{
//宣告結構體中的成員
string name;
int score;
int age;
struct Dog dog;
};
int main()
{
struct Dog dog = { "旺財",4 };
//將dog設定到student中
struct Student student = { "小明",90,10,dog };
//先獲取學生的指標
struct Student* student_p = &student;
//之后通過指標在獲取到學生內部的狗的地址賦值給狗指標
struct Dog* dog_p = &student_p->dog;
//通過狗指標獲取狗age
cout << dog_p->age;
}
結構體做函式引數
有兩種:值傳遞和參考傳遞
struct Dog {
string name;
int age;
};
//定義結構體
struct Student
{
//宣告結構體中的成員
string name;
int score;
int age;
struct Dog dog;
};
//值傳遞
void set_value(struct Student s1) {
s1.age = 100;
s1.dog.age = 10;
}
//參考傳遞
void set_value2(struct Student* s1) {
//設定學生年齡
s1->age = 100;
//設定狗的年齡
s1->dog.age = 10;
//這種也可以,通過指標操作
//struct Dog* dog_p = &(s1->dog);
//dog_p->age = 10;
}
int main()
{
struct Dog dog = { "旺財",4 };
struct Student student = { "小明",90,18,dog };
set_value2(&student);
cout << student.dog.age << endl;
cout << student.age << endl;
}
結構體中使用const
struct Dog {
string name;
int age;
};
//定義結構體
struct Student
{
//宣告結構體中的成員
string name;
int score;
int age;
struct Dog dog;
};
struct Dog dog = { "旺財",4 };
void print(const struct Student* s) {
struct Student student = { "老明",90,18,dog };
s = &student;
//s->name = "2";報錯
cout << s->name;
}
int main()
{
struct Student student = { "小明",90,18,dog };
print(&student);
}
tpis: 形參接收struct 例如 void print(const struct Student* s)
你這個Student拼寫錯了是不會報錯的,需要注意拼寫,或者直接省略
struct關鍵字
//定義結構體
struct Student
{
//宣告結構體中的成員
string name;
int score;
int age;
struct Dog dog;
};
struct Dog dog = { "旺財",4 };
void print(struct Student* const s) {
struct Student student = { "老明",90,18,dog };
//s = &student; 報錯
s->name = "王哈哈";
cout << s->name;
}
int main()
{
struct Student student = { "小明",90,18,dog };
print(&student);
}
結構體const和修飾普通的指標一樣
const struct Student* s 形參里面的值不能改變 但是指向的地址可以改變
struct Student* const s 形參里面值可以改變,但是指向不能改變
const struct Student* const s 兩個都不能變
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/519027.html
標籤:其他
