話不多說,序言搞起來: 自從開始學老師布置的任務后,目前還是OpenCV,哈~哈。我就莫名問老師:“以后編程是用C++還是python?”,果然還是太年輕,老師說:“兩們都要精通”。唉!于是乎為期兩周的C++編程入門學了一遍,雖然不難,但很詳細。
文章目錄
- C++基礎入門
- 1 C++初識
- 1.1 第一個C++程序
- 1.2 注釋
- 1.3 變量
- 1.4 常量
- 1.5 關鍵字
- 1.6 標識符命名規則
- 2、數據類型
- 2.1 整型
- 2.2 sizeof關鍵字
- 2.3 實型(浮點型)
- 2.4 字符型
- 2.5 轉義字符
- 2.6 字符串型
- 2.7 布爾類型bool
- 2.8 數據的輸入
- 3、運算符
- 3.1 算術運算符
- 3.2 賦值運算符
- 3.3 比較運算符
- 4 程序流程結構
- 4.1 選擇結構
- 4.1.1 if語句
- 4.1.2 三目運算符
- 4.1.3 switch語句
- 4.2 循環結構
- 4.2.1 while循環語句
- 4.2.2 do...while循環語句
- 4.2.3 for循環語句
- 4.2.4 嵌套循環
- 4.3 跳轉語句
- 4.3.1 break語句
- 4.3.2 continue語句
- 4.3.3 goto語句
- 5.數組
- 5.1 概述
- 5.2 一維數組
- 5.2.2一維數組數組名
- 5.3 二維數組
- 6 函數
- 6.1 概述
- 6.2 函數的定義
- 6.3 值傳遞
- 6.5 函數的常見形式
- 6.6 函數的聲明
- 6.7 函數的分文件編寫
- 7 指針
- 7.1 指針的基本概念
- 7.2 指針變量的定義和使用
- 7.3 指針所占用內存空間
- 7.4 空指針和野指針
- 7.5 const修飾指針
- 7.6 指針和數組
- 7.7 指針和函數
- 7.8 案例:利用冒泡升序排序
- 8 結構體
- 8.1 結構體基本概念
- 8.2 結構體的定義和使用
- 8.3 結構體數組
- 8.4 結構體指針
- 8.5 結構體嵌套結構體
- 8.6 結構體做函數參數
- 8.7 結構體中const使用場景
C++基礎入門
1 C++初識
1.1 第一個C++程序
#include<iostream>
using namespace std;
int main()
{cout << "hello world" << endl;system("pause");return 0;
}
1.2 注釋
-
單行注釋://
-
多行注釋:/**/
1.3 變量
數據類型 變量名 = 變量初始值;
1.4 常量
(1)作用:記錄程序中不可更改的數據
(2)定義常量的兩種方式:
- #define 宏常量:#define 常量名 常量值(通常定義在文件上方,一旦修改就會報錯)
- const修飾的變量:const 數據類型 常量名=常量值(通常在變量定義前加關鍵字const)
1.5 關鍵字
說明:在定義變量或者常量的時候,不能使用關鍵字
double\int\long\......
1.6 標識符命名規則
說明:給變量起名的時候要做到見名知意
(1)標識符不能是關鍵字
(2)標識符只能由字母、數字、下劃線
(3)第一個字符必須是字符或者下劃線
(4)標識符中區分大小寫
2、數據類型
說明:數據類型存在的意義是給變量分配合適的內存空間
2.1 整型
區別:在于所占內存空間不同
//短整型(-32768~32767)short num1 = 32769;//輸出就是一個負數-32768//整型int num2 = 10;//長整型long num3 = 10;//長長整型long long num4 = 10;cout << "num1= " << num1 << endl;cout << "num2= " << num2 << endl;cout << "num3= " << num3 << endl;cout << "num4= " << num4 << endl;
2.2 sizeof關鍵字
作用:利用sizeof關鍵字統計數據類型所占內存大小
b = sizeof(a);
2.3 實型(浮點型)
作用:表示小數
兩種表示形式:
- 1、單精度: float;占用空間:4字節;有效數字范圍:7位有效數字
- 2、雙精度:double;占用空間:8字節;有效數字范圍:15~16位有效數字
float f1 = 3.14f;//不加f會默認double類型的,會多一步轉化cout << f1 << endl;double d1 = 3.14;cout << d1 << endl;//統計float和double所占內存空間cout <<"float占用的內存空間:"<< sizeof(float) << endl;cout << "double占用的內存空間:" << sizeof(double) << endl;//科學計數法float f2 = 3e2;//3*10^2;cout << "f2=" << f2 << endl;float f3 = 3e-2;//3*0.1^2;cout << "f3=" << f3 << endl;
2.4 字符型
作用:顯示單個字符,只占用1個字節
語法:char ch=‘a’;
//1、字符型變量創建方式char ch = 'a';cout << ch << endl;//2、字符型變量所占內存空間大小cout << "char字符型所占內存:" << sizeof(ch) << endl;//3、字符型變量對應的ASCII編碼cout << (int)ch << endl;
2.5 轉義字符
作用:表示一些不能顯示出來的ASCII字符
- \n:換行
- \t:水平制表:\t占用8個字符,如aaa+5個空格;aaaa+4個空格
- \ \:代表反斜線字符“\”
2.6 字符串型
兩種形式:
- C:char 變量名[ ]=“字符串值”
- C++:string 變量名= “字符串值”
//C風格char str[] = "hello world";cout << str << endl;//C++風格string ss = "hello C++"; //需要一個#include<string>頭文件cout << ss << endl;
2.7 布爾類型bool
作用:代表真和假的值
bool類型只有兩個值,占1個字節大小
- true:真(本質是1)//非0的值都代表著真
- false:假(本質是0)
//1、創建bool類型bool flag = true;bool flag1 = false;cout << flag << endl;cout << flag1 << endl;//2、查看內存空間cout << "bool類型所占的內存空間:" << sizeof(flag) << endl;
2.8 數據的輸入
關鍵字:cin>>變量,從鍵盤上獲取數據
cout << "請輸入a的值:" << endl;
cin >> a;
3、運算符
3.1 算術運算符
注意:
- 兩數相除,除數不可以為0
- 兩個小數不可以進行取模運算
3.2 賦值運算符
3.3 比較運算符
4 程序流程結構
- 順序結構:程序按照順序執行,不發生跳轉
- 選擇結構:依據條件是否滿足,有選擇的執行相應的功能
- 循環結構:依據條件是否滿足,循環多次執行某段代碼
4.1 選擇結構
4.1.1 if語句
#include<iostream>
using namespace std;
int main()
{system("color 5F");cout << "請輸入一個分數:" << endl;int score;cin >> score;//注意:if條件后面不要加分號if (score > 600)printf("你考上一本大學\n");else if(score>500)printf("你考上了二本\n");elseprintf("你什么都沒有考上,二戰吧\n");return 0;
}
嵌套的if語句:
案例需求:如果上述大于600考上了一本大學,如果分數大于650就可以考上雙一流
if (score > 600){printf("你考上一本大學\n");if (score > 650){printf("可以上雙一流了\n");}}else if (score > 500)printf("你考上了二本\n");elseprintf("你什么都沒有考上,二戰吧\n");
4.1.2 三目運算符
int a = 10;int b = 20;int c = 0;c = (a > b ? a : b);cout << "c= " << c << endl;//C++中三目運算符返回的是變量,可以繼續賦值(a > b ? a : b) = 66;cout << "a= " << a << endl;cout << "b= " << b << endl;
4.1.3 switch語句
語法:
switch(表達式)
{case 結果1:執行語句;break;case 結果2:執行語句;break;....default:執行語句;break;
}
示例:給電影打分
cout << "請給電影打分:" << endl;int score = 0;cin >> score;switch (score){case 3: cout << "你打的電影評分是:" << score << endl; break;case 6: cout << "你打的電影評分是:" << score << endl; break;case 9: cout << "你打的電影評分是:" << score << endl; break;case 4: cout << "你打的電影評分是:" << score << endl; break;default:cout << "你打的是其他分數:" << score << endl; break;}
4.2 循環結構
4.2.1 while循環語句
語法:while(循環條件){循環語句}
注意:了解循環條件,避免死循環
//在屏幕打印0-9int num = 0;while (num < 10){cout << num << endl;num++;}
4.2.2 do…while循環語句
語法:do{循環語句}while{循環條件}與while的區別是先執行一次循環語句,再判斷循環條件
int num = 0;
do
{cout << num << endl;num++;
} while (num < 10);
4.2.3 for循環語句
語法:for(起始表達式;條件表達式;末尾循環體){ 循環語句;}
//在屏幕打印0-9
for (int i = 0; i < 10; i++)
{cout << i << endl;
}
4.2.4 嵌套循環
作用:循環中再加一層循環
//在屏幕打印10x10的*陣for (int i = 0; i < 10; i++){for (int j = 0; j < 10; j++){cout <<"* ";}cout << endl;}
示例:打印乘法口訣表
#include<iostream>
using namespace std;
int main()
{system("color 5F");for (int i = 1; i < 10; i++){for (int j = 1; j <=i; j++){cout << j << " x " << i << " = " << (i*j)<<" ";}cout << endl;}return 0;
}
4.3 跳轉語句
4.3.1 break語句
作用:結束循環
//在屏幕打印10x5的*陣for (int i = 0; i < 10; i++){for (int j = 0; j < 10; j++){if(j==5)break;cout <<"* ";}cout << endl;}
4.3.2 continue語句
作用:在循環語句中,跳過本次循環中余下尚未執行的語句,繼續執行下一次循環
//在屏幕輸出10以內偶數for (int i = 0; i < 10; i++){if (i % 2 != 0)continue;cout << i << endl;}
4.3.3 goto語句
作用:goto標記之后,跳轉到標記的語句
#include<iostream>
using namespace std;
int main()
{system("color 5F");//執行goto語句cout << "1.xxxxx" << endl;goto Flag;cout << "2.xxxxx" << endl;cout << "3.xxxxx" << endl;cout << "4.xxxxx" << endl;Flag:cout << "5.xxxxx" << endl;return 0;
}
5.數組
5.1 概述
所謂數組,就是一個集合,里面存放了相同類型的數據元素
5.2 一維數組
1、定義方式
- 數據類型 數組名[數組長度];
- 數據類型 數據名[數組長度]={值1,值2…};
- 數據類型 數組名[ ]={值1,值2…};
2、數組特點
放在一塊連續的內存空間,數組中每個元素都是相同的數據類型。
int main()
{system("color 5F");//1、數據類型 數組名[數組長度];int arr[5];arr[0] = 10;arr[1] = 20;arr[2] = 30;arr[3] = 40;arr[4] = 50;for (int i = 0; i < 5; i++){cout << arr[i] << " ";}cout << endl;//2、數據類型 數據名[數組長度]={值1,值2.....};//若初始化沒有五個數據,剩下的將會由0填補int arr1[5] = { 10,20,30,40};for (int i = 0; i < 5; i++){cout << arr1[i] << " ";}cout << endl;//3、數據類型 數組名[ ]={值1,值2.....};int arr3[] = { 1,2,3,4,5 ,6,5,8,9};for (int i = 0; i < 9; i++){cout << arr3[i] << " ";}cout << endl;return 0;
}
5.2.2一維數組數組名
用途:
- 統計整個數組在內存中的長度
- 可以獲取數組在內存中的首地址
#include<iostream>
using namespace std;
int main()
{system("color 5F");int arr[5] = { 1,2,3,4,5 };//1、通過數組名統計整個數組占用內存大小cout << "每個數組占用內存空間大小:"<<sizeof(arr) << endl;cout << "每個元素所占用的內存空間大小:" << sizeof(arr[0]) << endl;cout << "數組中元素的個數為:" << sizeof(arr)/ sizeof(arr[0]) << endl;//2、通過數組名查看首地址cout << "數組首地址為:" << (int)arr << endl;cout << "數組中第一個元素地址:" << (int)&arr[1] << endl;return 0;
}
練習案例:數組元素逆置
請聲明一個5個元素的數組,并且將元素逆置
#include<iostream>
using namespace std;
int main()
{system("color 5F");int arr[5] = { 1,2,3,4,5 };//1、數組逆置前序列cout << "數組逆置前序列:" << endl;for (int i = 0; i < 5; i++){cout << arr[i] << " ";}cout << endl;//2、實現逆置int start = 0;int end = sizeof(arr) / sizeof(arr[0])-1;while (start < end){int temp = arr[start];arr[start] = arr[end];arr[end] = temp;start++;end--;}cout << "數組逆置之后輸出:" << endl;for (int i = 0; i < 5; i++){cout << arr[i] << " ";}cout << endl;return 0;
}
5.3 二維數組
1、二維數組的四種定義類型:
- 數據類型 數組名[行數] [列數];
- 數據類型 數組名[行數] [列數]={{數據1,數據2},{數據3,數據4}};
- 數據類型 數組名[行數] [列數]={數據1,數據2,數據3,數據4};
- 數據類型 數組名[ ] [列數]={數據1,數據2,數據3,數據4};
#include<iostream>
using namespace std;
int main()
{system("color 5F");/*- 數據類型 數組名[行數] [列數];- 數據類型 數組名[行數] [列數]={{數據1,數據2},{數據3,數據4}};- 數據類型 數組名[行數] [列數]={數據1,數據2,數據3,數據4};- 數據類型 數組名[ ] [列數]={數據1,數據2,數據3,數據4};*///1、數據類型 數組名[行數] [列數];int arr[2][3];arr[0][0] = 1;arr[0][1] = 2;arr[0][2] = 3;arr[1][0] = 4;arr[1][1] = 5;arr[1][2] = 6;for (int i = 0; i < 2; i++){for (int j = 0; j < 3; j++){cout << arr[i][j] << " ";}cout << endl;}cout << endl;//2、數據類型 數組名[行數] [列數]={{數據1,數據2},{數據3,數據4}};int arr2[2][3]={{1,2,3},{4,5,6}};for (int i = 0; i < 2; i++){for (int j = 0; j < 3; j++){cout << arr[i][j] << " ";}cout << endl;}cout << endl;//3、數據類型 數組名[行數] [列數]={數據1,數據2,數據3,數據4};int arr3[2][3] = { 1,2,3,4,5,6 };for (int i = 0; i < 2; i++){for (int j = 0; j < 3; j++){cout << arr[i][j] << " ";}cout << endl;}//4、數據類型 數組名[ ] [列數]={數據1,數據2,數據3,數據4};int arr4[][3] = { 1,2,3,4,5,6 };return 0;
}
2、二維數組數組名
int arr[2][3]={{1,2,3},{4,5,6}};//1、查看占用內存空間大小cout << "二維數組占用內存空間為:" << sizeof(arr) << endl;cout << "二維數組第一行占用的內存:" << sizeof(arr[0]) << endl;cout << "二維數組第一元素占用的內存:" << sizeof(arr[0][0]) << endl;cout << "二維數組的行數為:" << sizeof(arr) / sizeof(arr[0]) << endl;cout << "二維數組的列數為:" << sizeof(arr[0]) / sizeof(arr[0][0]) << endl;//2、查看二維數組的首地址cout << " 二維數組首地址: " << (int)arr << endl;cout << " 二維數組第一行元素首地址: " << (int)arr[0] << endl;cout << " 二維數組第二行元素首地址: " << (int)arr[1] << endl;
6 函數
6.1 概述
說明:將一段經常使用的代碼封裝起來,減少代碼重復
6.2 函數的定義
步驟:
- 1、返回值類型
- 2、函數名
- 3、參數列表
- 4、函數體語句
- 5、return表達式
返回值類型 函數名 (參數列表)
{函數體語句;return 表達式;
}
#include<iostream>
using namespace std;
int add(int num1, int num2)
{//num1、num2沒有真正的數據。是一個形式參數,也叫形參int sum = num1 + num2;return sum;
}
int main()
{system("color 5F");//a、b有實際的值,叫實參//當函數調用時,實參的值會傳遞給形參int a, b;cin >> a >> b;cout <<a<<" + "<<b<< " = " << add(a, b) << endl;return 0;
}
6.3 值傳遞
- 值傳遞就是函數調用時實參將數值傳給形參
- 值傳遞:如果形參發生,并不影響實參
#include<iostream>
using namespace std;
//值傳遞
//實現兩個數字進行交換的函數//若函數不需要返回值,聲明的時候可以寫void
void swap(int num1, int num2)
{cout << "交換前:" << endl;cout << "num1=" << num1 << endl;cout << "num2=" << num2 << endl;int temp;temp = num1;num1 = num2;num2 = temp;cout << "交換后:" << endl;cout << "num1=" << num1 << endl;cout << "num2=" << num2 << endl;}
int main()
{system("color 5F");int a = 10, b = 20;//當做值傳遞的時候,函數的形參發生改變,不會影響實參swap(a, b);return 0;
}
6.5 函數的常見形式
- 1、無參無返
- 2、有參無返
- 3、無參有返
- 4、有參有返
#include<iostream>
using namespace std;
//1、無參無返
void test01()
{cout << "this is 01" << endl;
}
//2、有參無返
void test02(int a)
{cout << "this is 02 a=" << a << endl;
}
//3、無參有返
int test03()
{cout << "this is 03" << endl;return 666;
}
//4、有參有返
int test04(int a)
{cout << "this is 04 a=" << a << endl;return 888;
}
int main()
{system("color 5F");//無參無返函數調用test01();//有參無返函數調用test02(3);//無參有返函數調用int num = test03();cout << "無參有返的num=" << num << endl;//有參有返函數調用cout << "有參有返的num=" << test04(20) << endl;return 0;
}
6.6 函數的聲明
作用:提前告訴編譯器函數的存在,聲明可以寫多次,定義只能寫一次。聲明是說明存在這個函數,定義是說明這個函數如何寫。
6.7 函數的分文件編寫
作用:讓代碼結構更加清晰
步驟:
- 1、創建后綴名為.h的頭文件
- 2、創建后綴名為.cpp的源文件
- 3、在頭文件中寫函數的聲明
- 4、在源文件中寫函數的定義
//在頭文件中寫函數的聲明
#pragma once
#include<iostream>
using namespace std;
//函數聲明
void swap(int a, int b);
//在源文件中寫函數的定義
#include"swap.h" //雙引號是自定義頭文件
void swap(int a, int b)
{int temp = a;a = b;b = temp;cout << "a= " << a << endl;cout << "b= " << b << endl;
}
//調用之后直接運行
#include<iostream>
#include"swap.h"
using namespace std;
int main()
{system("color 5F");int a = 10;int b = 20;swap(a, b);return 0;
}
7 指針
7.1 指針的基本概念
作用:可以通過指針間接訪問內存
- 內存編號從0開始記錄,一般用十六進制數字表示
- 可以利用指針變量保存地址
7.2 指針變量的定義和使用
指針定義語法: 數據類型 * 變量名;
#include<iostream>
using namespace std;
int main()
{system("color 5E");//1、定義指針int a = 10;//指針定義的語法:數據類型 * 指針變量名int * p;//讓指針記錄變量a的地址p = &a;cout << "a的地址為:" << p << endl;//2、使用指針//可以通過解引用的方式來找到指針指向的內存//指針前加*代表解引用,找到指針指向的內存中的數據cout << "a的值是:" << *p << endl;if (a == *p)cout << "一樣" << endl;elsecout << "不一樣" << endl;return 0;
}
7.3 指針所占用內存空間
問: 指針也是種數據類型,那么占用多少內存呢?
答: 在32位操作系統下:占4個字節空間;在64為操作系統:占8個字節
#include<iostream>
using namespace std;
int main()
{system("color 5E");int a = 10;int *p;p = &a;cout << "sizeof(int *)=" << sizeof(int *) << endl;cout << "sizeof(double *)=" << sizeof(double *) << endl;cout << "sizeof(char *)=" << sizeof(char *) << endl;cout << "sizeof(float *)=" << sizeof(float *) << endl;return 0;
}
7.4 空指針和野指針
(1)空指針:指針變量指向內存中編號為0的空間
用途:初始化指針變量
注意:空指針指向的內存是不可以訪問的
(2)野指針:指針變量指向非法的內存空間
7.5 const修飾指針
const修飾指針有三種情況:
-
(1)const修飾指針:常量指針
const int *p=&a;
特點:指針的指向可以修改,但是指針指向的值不可以修改
*p=20(×)
p=&b(√)
-
(2)const修飾常量:指針常量
int * const p=&a;
特點:指針指向不可以改,指針指向的值可以改
*p=20(√)
p=&b(×)
-
(3)const即修飾指針又修飾常量
const int * const p=&a;
特點:指針指向不可以改,指針指向的值也不可以改
*p=20(x)
p=&b(×)
7.6 指針和數組
作用:利用指針訪問數組元素
int arr[10] = { 1,2,3,4,5,6,7,8,9 };int *p = arr;//arr就是數組的首地址cout << "利用指針指向第一個元素:" << *p << endl;p++;//指針偏移4個字節cout << "利用指針指向第二個元素:" << *p << endl;for(int i=2;i<10;i++){cout << *p << " ";p++;}
7.7 指針和函數
作用:利用指針作為函數參數,可以修改實參的值
#include<iostream>
using namespace std;
void swap(int *p1, int *p2)
{int temp = *p1;*p1 = *p2;*p2 = temp;cout << "swap *p1=" << *p1 << endl;cout << "swap *p2=" << *p2 << endl;
}
int main()
{system("color 5E");//地址傳遞int a = 10;int b = 20;cout << "a= " << a << endl;cout << "b= " << b << endl;swap(&a, &b);cout << endl << "交換后的a,b的值:" << endl;cout << "a= " << a << endl;cout << "b= " << b << endl;return 0;
}
7.8 案例:利用冒泡升序排序
序列:int arr[10] = { 4,2,3,6,1,8,7,9,5,1 };
#include<iostream>
using namespace std;
void bubbleSort(int *arr, int len)
{for (int i = 0; i < len; i++){for (int j = 0; j < len-i-1; j++){if (arr[j] > arr[j + 1]){int temp = arr[j];arr[j] = arr[j + 1];arr[j + 1] = temp;}}}}
int main()
{//system("color 1E");//創建一個數組int arr[ ] = { 4,2,3,6,1,5,8,7,9,1 };//求數組的長度int len = sizeof(arr) / sizeof(arr[0]);//排序前的序列cout << "排序前的序列:" << endl;for (int i = 0; i < len; i++)cout << arr[i] << " ";cout << endl;//利用冒泡排序bubbleSort(arr, len);cout << "排序后的序列:" << endl;for (int i = 0; i < len ; i++)cout << arr[i] << " ";cout << endl;return 0;
}
8 結構體
8.1 結構體基本概念
結構體屬于用戶自定義的數據類型,允許用戶存儲不同的數據類型。
8.2 結構體的定義和使用
語法:struct 結構體名 {結構體成員列表};
結構體創建的方式三種:
- (1)struct 結構體名 變量名
- (2)struct 結構體名 變量名={成員1值,成員2值…}
- (3)定義結構體是順便創建變量
#include<iostream>
#include<string>
using namespace std;
//創建學生數據類型:學生包括(姓名,年齡,分數)
struct Student
{//成員列表//姓名string name;//年齡int age;//分數int score;
}s3; //3、定義結構體是順便創建變量int main()
{system("color 5E");//1、struct 結構體名 變量名struct Student s1;s1.name = "張鐵蛋";s1.age= 23;s1.score = 100;cout << "姓名:" << s1.name << " 年齡:" << s1.age << " 分數:" << s1.score << endl;//2、struct 結構體名 變量名={成員1值,成員2值......}struct Student s2 = { "徐鐵柱",24,100 };cout << "姓名:" << s2.name << " 年齡:" << s2.age << " 分數:" << s2.score << endl;s3.name = "憨憨";s3.age = 23;s3.score = 99;cout << "姓名:" << s3.name << " 年齡:" << s3.age << " 分數:" << s3.score << endl;return 0;
}
注意:結構體定義的時候不能省略struct,創建變量的時候可以省略
8.3 結構體數組
作用:將自定義的結構體放入到數組中方便維護
語法:struct 結構體名 數組名[元素個數]={{},{},{}…}
int main()
{system("color 5E");//創建結構體數組struct Student arr[3] ={{"張鐵蛋",18,100},{"徐鐵住",23,99},{"憨憨",22,90}};arr[2].name = "徐傻傻";for(int i=0;i<3;i++)cout << "姓名:" << arr[i].name << " 年齡:" << arr[i].age << " 分數:" << arr[i].score << endl;return 0;
}
8.4 結構體指針
作用:通過指針訪問結構體中的成員
- 利用操作符“->”可以通過結構體指針訪問結構體屬性
//創建結構體變量struct student s = { "張三",18,100 };//通過指針指向結構體變量struct student *p=&s;//通過指針訪問結構體中的數據p->name = "鐵蛋";cout << "姓名:" << p->name << " 年齡:" << p->age << " 分數:" << p->score << endl;
8.5 結構體嵌套結構體
作用:結構體的成員可以是另一個結構體
如:每個老師輔導一個學員,一個老師是一個結構體,記錄一個學生結構體
#include<iostream>
#include<string>
using namespace std;
//定義學生結構體
struct student
{int num;//學號string name;int age;
};
//定義老師結構體
struct teacher
{int id;string name;int age;struct student stu;
};
int main()
{system("color 5E");//創建老師、學生teacher ter = { 2311,"王老師",35,{20180505,"張三",22} };cout << "老師的編號:" << ter.id << endl;cout << "老師的名字:" << ter.name << endl;cout << "老師的年齡:" << ter.age << endl;cout << "老師的學生學號:" << ter.stu.num << endl;cout << "老師的學生的名字:" << ter.stu.name << endl;cout << "老師的學生的年齡:" << ter.stu.age << endl;return 0;
}
8.6 結構體做函數參數
作用:將結構體作為參數向函數中傳遞
傳遞的方式:
- 值傳遞
- 地址傳遞
#include<iostream>
#include<string>
using namespace std;
//定義學生結構體
struct student
{string name;int age;int score;
};
//值傳遞
void printStudent(struct student s)
{s.age = 55;cout << "子函數1打印的結果:" << endl;cout << "姓名:" << s.name << " 年齡:" << s.age << " 分數:" << s.score << endl;
}
//地址傳遞
void printfStudent1(struct student *s)
{cout << "子函數2打印的結果:" << endl;cout << "姓名:" << s->name << " 年齡:" << s->age << " 分數:" << s->score << endl;s->name = "張鐵蛋";s->score = 999;}
int main()
{system("color 5E");student s = { "張三",23,99 };printStudent(s);cout << "main打印的結果:" << endl;cout << "姓名:" << s.name << " 年齡:" << s.age << " 分數:" << s.score << endl;cout << endl;printfStudent1(&s);cout << "main打印的結果:" << endl;cout << "姓名:" << s.name << " 年齡:" << s.age << " 分數:" << s.score << endl;return 0;
}
8.7 結構體中const使用場景
作用:用const來防止誤操作
#include<iostream>
#include<string>
using namespace std;
//定義學生結構體
struct student
{string name;int age;int score;
};
//將函數中的形參改為指針,可以減少內存空間,而且不會復制新的副本
void printStudent(const struct student *s)
{//s->age = 55;//加入const之后,一旦有修改的操作就會報錯,防止誤操作cout << "姓名:" << s->name << " 年齡:" << s->age << " 分數:" << s->score << endl;
}
int main()
{system("color 5E");student s = { "張三",23,99 };//只讀,不可修改printStudent(&s);return 0;
}