主頁 > 後端開發 > C++基礎

C++基礎

2022-10-24 07:12:16 後端開發

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

運算子

  1. 算數運算子 處理四則運算
  2. 賦值運算子 將運算式賦值給變數
  3. 比較運算子 運算式的比較并回傳一個bool值
  4. 邏輯運算子 用于根據運算式的值回傳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;
	
}

陣列名用途

  1. 可以獲取整個陣列在記憶體中的長度
  2. 可以獲取記憶體中陣列的首地址
#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];
		}
	}

}

函式(方法)

一個函式包含以下內容:

  1. 回傳值型別
  2. 函式名稱
  3. 引數串列
  4. 函式體
  5. 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,或者我們輸出number1a的地址,也會發現不同,說明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;
}

函式分檔案撰寫

將相同功能的函式單獨放在一個檔案中,使結構更加清晰

  1. 創建后綴為.h 的頭檔案
  2. 創建后綴為.cpp的源檔案
  3. 在頭檔案中寫函式宣告
  4. 在源檔案中寫函式實作

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有三種情況

  1. 修飾指標:常量指標
  2. 修飾常量:指標常量
  3. 即修飾指標,又修飾常量

常量指標

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: 注意! 結構體結束}后面需要加一個;結尾

通過結構體創建變數的方式有三種:

  1. struct 結構體名 變數名
  2. struct 結構體名 變數名={成員1值,成員2值}
  3. 定義結構體時同時設定變數

方式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

標籤:其他

上一篇:django中APIView里的dispatch和as_view方法分析

下一篇:SpringMCV(八):檔案上傳

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • 【C++】Microsoft C++、C 和匯編程式檔案

    ......

    uj5u.com 2020-09-10 00:57:23 more
  • 例外宣告

    相比于斷言適用于排除邏輯上不可能存在的狀態,例外通常是用于邏輯上可能發生的錯誤。 例外宣告 Item 1:當函式不可能拋出例外或不能接受拋出例外時,使用noexcept 理由 如果不打算拋出例外的話,程式就會認為無法處理這種錯誤,并且應當盡早終止,如此可以有效地阻止例外的傳播與擴散。 示例 //不可 ......

    uj5u.com 2020-09-10 00:57:27 more
  • Codeforces 1400E Clear the Multiset(貪心 + 分治)

    鏈接:https://codeforces.com/problemset/problem/1400/E 來源:Codeforces 思路:給你一個陣列,現在你可以進行兩種操作,操作1:將一段沒有 0 的區間進行減一的操作,操作2:將 i 位置上的元素歸零。最終問:將這個陣列的全部元素歸零后操作的最少 ......

    uj5u.com 2020-09-10 00:57:30 more
  • UVA11610 【Reverse Prime】

    本人看到此題沒有翻譯,就附帶了一個自己的翻譯版本 思考 這一題,它的第一個要求是找出所有 $7$ 位反向質數及其質因數的個數。 我們應該需要質數篩篩選1~$10^{7}$的所有數,這里就不慢慢介紹了。但是,重讀題,我們突然發現反向質數都是 $7$ 位,而將它反過來后的數字卻是 $6$ 位數,這就說明 ......

    uj5u.com 2020-09-10 00:57:36 more
  • 統計區間素數數量

    1 #pragma GCC optimize(2) 2 #include <bits/stdc++.h> 3 using namespace std; 4 bool isprime[1000000010]; 5 vector<int> prime; 6 inline int getlist(int ......

    uj5u.com 2020-09-10 00:57:47 more
  • C/C++編程筆記:C++中的 const 變數詳解,教你正確認識const用法

    1、C中的const 1、區域const變數存放在堆疊區中,會分配記憶體(也就是說可以通過地址間接修改變數的值)。測驗代碼如下: 運行結果: 2、全域const變數存放在只讀資料段(不能通過地址修改,會發生寫入錯誤), 默認為外部聯編,可以給其他源檔案使用(需要用extern關鍵字修飾) 運行結果: ......

    uj5u.com 2020-09-10 00:58:04 more
  • 【C++犯錯記錄】VS2019 MFC添加資源不懂如何修改資源宏ID

    1. 首先在資源視圖中,添加資源 2. 點擊新添加的資源,復制自動生成的ID 3. 在解決方案資源管理器中找到Resource.h檔案,編輯,使用整個專案搜索和替換的方式快速替換 宏宣告 4. Ctrl+Shift+F 全域搜索,點擊查找全部,然后逐個替換 5. 為什么使用搜索替換而不使用屬性視窗直 ......

    uj5u.com 2020-09-10 00:59:11 more
  • 【C++犯錯記錄】VS2019 MFC不懂的批量添加資源

    1. 打開資源頭檔案Resource.h,在其中預先定義好宏 ID(不清楚其實ID值應該設定多少,可以先新建一個相同的資源項,再在這個資源的ID值的基礎上遞增即可) 2. 在資源視圖中選中專案資源,按F7編輯資源檔案,按 ID 型別 相對路徑的形式添加 資源。(別忘了先把檔案拷貝到專案中的res檔案 ......

    uj5u.com 2020-09-10 01:00:19 more
  • C/C++編程筆記:關于C++的參考型別,專供新手入門使用

    今天要講的是C++中我最喜歡的一個用法——參考,也叫別名。 參考就是給一個變數名取一個變數名,方便我們間接地使用這個變數。我們可以給一個變數創建N個參考,這N + 1個變數共享了同一塊記憶體區域。(參考型別的變數會占用記憶體空間,占用的記憶體空間的大小和指標型別的大小是相同的。雖然參考是一個物件的別名,但 ......

    uj5u.com 2020-09-10 01:00:22 more
  • 【C/C++編程筆記】從頭開始學習C ++:初學者完整指南

    眾所周知,C ++的學習曲線陡峭,但是花時間學習這種語言將為您的職業帶來奇跡,并使您與其他開發人員區分開。您會更輕松地學習新語言,形成真正的解決問題的技能,并在編程的基礎上打下堅實的基礎。 C ++將幫助您養成良好的編程習慣(即清晰一致的編碼風格,在撰寫代碼時注釋代碼,并限制類內部的可見性),并且由 ......

    uj5u.com 2020-09-10 01:00:41 more
最新发布
  • Rust中的智能指標:Box<T> Rc<T> Arc<T> Cell<T> RefCell<T> Weak

    Rust中的智能指標是什么 智能指標(smart pointers)是一類資料結構,是擁有資料所有權和額外功能的指標。是指標的進一步發展 指標(pointer)是一個包含記憶體地址的變數的通用概念。這個地址參考,或 ” 指向”(points at)一些其 他資料 。參考以 & 符號為標志并借用了他們所 ......

    uj5u.com 2023-04-20 07:24:10 more
  • Java的值傳遞和參考傳遞

    值傳遞不會改變本身,參考傳遞(如果傳遞的值需要實體化到堆里)如果發生修改了會改變本身。 1.基本資料型別都是值傳遞 package com.example.basic; public class Test { public static void main(String[] args) { int ......

    uj5u.com 2023-04-20 07:24:04 more
  • [2]SpinalHDL教程——Scala簡單入門

    第一個 Scala 程式 shell里面輸入 $ scala scala> 1 + 1 res0: Int = 2 scala> println("Hello World!") Hello World! 檔案形式 object HelloWorld { /* 這是我的第一個 Scala 程式 * 以 ......

    uj5u.com 2023-04-20 07:23:58 more
  • 理解函式指標和回呼函式

    理解 函式指標 指向函式的指標。比如: 理解函式指標的偽代碼 void (*p)(int type, char *data); // 定義一個函式指標p void func(int type, char *data); // 宣告一個函式func p = func; // 將指標p指向函式func ......

    uj5u.com 2023-04-20 07:23:52 more
  • Django筆記二十五之資料庫函式之日期函式

    本文首發于公眾號:Hunter后端 原文鏈接:Django筆記二十五之資料庫函式之日期函式 日期函式主要介紹兩個大類,Extract() 和 Trunc() Extract() 函式作用是提取日期,比如我們可以提取一個日期欄位的年份,月份,日等資料 Trunc() 的作用則是截取,比如 2022-0 ......

    uj5u.com 2023-04-20 07:23:45 more
  • 一天吃透JVM面試八股文

    什么是JVM? JVM,全稱Java Virtual Machine(Java虛擬機),是通過在實際的計算機上仿真模擬各種計算機功能來實作的。由一套位元組碼指令集、一組暫存器、一個堆疊、一個垃圾回收堆和一個存盤方法域等組成。JVM屏蔽了與作業系統平臺相關的資訊,使得Java程式只需要生成在Java虛擬機 ......

    uj5u.com 2023-04-20 07:23:31 more
  • 使用Java接入小程式訂閱訊息!

    更新完微信服務號的模板訊息之后,我又趕緊把微信小程式的訂閱訊息給實作了!之前我一直以為微信小程式也是要企業才能申請,沒想到小程式個人就能申請。 訊息推送平臺🔥推送下發【郵件】【短信】【微信服務號】【微信小程式】【企業微信】【釘釘】等訊息型別。 https://gitee.com/zhongfuch ......

    uj5u.com 2023-04-20 07:22:59 more
  • java -- 緩沖流、轉換流、序列化流

    緩沖流 緩沖流, 也叫高效流, 按照資料型別分類: 位元組緩沖流:BufferedInputStream,BufferedOutputStream 字符緩沖流:BufferedReader,BufferedWriter 緩沖流的基本原理,是在創建流物件時,會創建一個內置的默認大小的緩沖區陣列,通過緩沖 ......

    uj5u.com 2023-04-20 07:22:49 more
  • Java-SpringBoot-Range請求頭設定實作視頻分段傳輸

    老實說,人太懶了,現在基本都不喜歡寫筆記了,但是網上有關Range請求頭的文章都太水了 下面是抄的一段StackOverflow的代碼...自己大修改過的,寫的注釋挺全的,應該直接看得懂,就不解釋了 寫的不好...只是希望能給視頻網站開發的新手一點點幫助吧. 業務場景:視頻分段傳輸、視頻多段傳輸(理 ......

    uj5u.com 2023-04-20 07:22:42 more
  • Windows 10開發教程_編程入門自學教程_菜鳥教程-免費教程分享

    教程簡介 Windows 10開發入門教程 - 從簡單的步驟了解Windows 10開發,從基本到高級概念,包括簡介,UWP,第一個應用程式,商店,XAML控制元件,資料系結,XAML性能,自適應設計,自適應UI,自適應代碼,檔案管理,SQLite資料庫,應用程式到應用程式通信,應用程式本地化,應用程式 ......

    uj5u.com 2023-04-20 07:22:35 more