1. 程式人生 > 其它 >C++學習 3 結構體

C++學習 3 結構體

結構體基本概念:

  結構體屬於使用者自定義的資料型別,允許使用者儲存不同的資料型別;

結構體定義和使用:

  語法:struct結構體名 {結構體成員列表 };

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

    1、struct結構體名變數名

    2、struct結構體名變數名 = {成員1值,成員2值...}

    3、定義結構體時順便建立變數

   例項:

#include<iostream>
#include<string>
using namespace std;

//1、建立學生資料型別  :  學生包括(姓名、年齡、分數)

struct
Student { //成員列表 string name; //姓名 int age; //年齡 int score; //分數 }s3; //順便建立結構體變數
//2、通過學生型別建立具體學生
int main() { //2.1 struct Student s1 //struct 關鍵字 在C++中可以省略 相當於下行 Student s1;//struct Student s1; //給s1賦值 s1.name = "張三"; s1.age = 18; s1.score = 100; cout << "
姓名:" << s1.name << " " << "年齡:" << s1.age << " " << "分數:" << s1.score << endl; //2.2 struct Student s2 = {...} struct Student s2 = { "李四",19,80 }; cout << "姓名:" << s2.name << " " << "年齡:" << s2.age << " " << "
分數:" << s2.score << endl; //2.3 在定義結構體時順便建立變數 s3.name = "王五"; s3.age = 30; s3.score = 60; cout << "姓名:" << s3.name << " " << "年齡:" << s3.age << " " << "分數:" << s3.score << endl; system("pause"); return 0; }

結構體陣列:

作用:將自定義的結構體放入到陣列中方便維護;

語法:struct結構體名陣列名{元素個數} = { { },{ },......{ } }

例項:

#include<iostream>
#include<string>
using namespace std;

//結構體陣列
//1、定義結構體

struct Student
{
    string name;  //姓名
    int age;      //年齡
    int score;    //分數
};

int main()
{
//2、建立結構體陣列
    struct Student stuArray[3] =
    {
        {"張三",18,100},
        {"李四",28,99},
        {"王五",38,66}
    };
//3、給結構體陣列中的元素賦值
    stuArray[2].name = "趙六";    //將王五的資訊改成趙六的
    stuArray[2].age = 80;
    stuArray[2].score = 60;
//4、遍歷結構體陣列
    for (int i = 0; i < 3; i++)
    {
        cout << "姓名:" << stuArray[i].name << "  " 
             << "年齡:" << stuArray[i].age <<"  " 
             << "分數:" << stuArray[i].score << endl;
    }
    system("pause");
    return 0;
}

結構體指標

作用:通過指標訪問結構體中的成員;利用操作符 ->可以通過結構體指標訪問結構體屬性。

例項:

#include<iostream>
#include<string>
using namespace std;

//結構體指標
//1、定義學生結構體

struct Student
{
    string name;  //姓名
    int age;      //年齡
    int score;    //分數
};

int main()
{
//2、建立學生結構體變數
    //struct Student s = {"張三",18,100};  //struct可以省略
    Student s = {"張三",18,100};
    
//3、通過指標指向結構體變數
    //int * p = &s;   ->!!! 錯誤!!! <-  因為上行程式碼指定s是Student型別的,所以不能返回int型的資料,而應該返回Student型,即不能用int型的指標指向s;
    
    //struct Student * p = &s;      //struct可以省略
    Student * p = &s;

//4、通過指標訪問結構體中的資料
    cout << "姓名:" << p->name <<"  " << "年齡:" << p->age <<"  " << "分數" << p->score <<endl;

    system("pause");
    return 0;
}

結構體巢狀結構體

作用:結構體中的成員可以是另一個結構體

例如:每個老師輔導一個學員,一個老師的結構體中,記錄一個學生的結構體

例項:

#include<iostream>
#include<string>
using namespace std;

//結構體巢狀結構體

//定義學生結構體
struct Student
{
    string name;    //姓名
    int age;        //年齡
    int score;        //分數
};

//定義老師結構體
struct Teacher
{
    int id;                  //教職工編號
    string name;          //姓名
    int age;              //年齡
    struct Student stu;   //所帶的學生
};

int main()
{
    Teacher t;
    t.id = 10000;
    t.name = "老王";
    t.age = 50;
    t.stu.name = "小王";
    t.stu.age = 20;
    t.stu.score = 60;

    cout << "老師姓名:" << t.name <<"  " << "老師編號:" << t.id << "  " << "老師年齡:" << t.age << "  "
         << "老師輔導的學生姓名:" << t.stu.name << "  " << "學生分數:" << t.stu.score << "  " << "學生年齡:" << t.stu.age << endl;

    system("pause");
    return 0;
}

結構體做函式引數

作用:將結構體作為引數向函式中傳遞

傳遞的方式有兩種:1、值傳遞;2、地址傳遞;

例項:

#include<iostream>
#include<string>
using namespace std;

//結構體做函式引數

//定義學生結構體
struct Student
{
    string name;    //姓名
    int age;        //年齡
    int score;        //分數
};

//1、值傳遞
void printStudent1(struct Student s)
{
    cout << "子函式中通過值傳遞列印 姓名:" << s.name <<"  " << "年齡:" << s.age <<"  " << "分數:" << s.score << endl;
}
//2、地址傳遞
void printStudent2(struct Student * p)
{
    cout << "子函式中通過地址傳遞列印 姓名:" << p->name << "  " << "年齡:" << p->age << "  " << "分數:" << p->score << endl;
}
int main()
{
//將學生傳入到一個引數中,列印學生身上的所有資訊
    struct Student s;
    s.name = "小王";
    s.age = 20;
    s.score = 99;

    //cout << "main函式中列印 姓名:" << s.name <<"  " << "年齡:" << s.age <<"  " << "分數:" << s.score << endl;
    //printStudent1(s);
    printStudent2(&s);
    system("pause");
    return 0;
}

值傳遞和地址傳遞區別:

值傳遞:

void printStudent1(struct Student s)
{
    s.age = 100;
    cout << "子函式中通過值傳遞列印 姓名:" << s.name <<"  " << "年齡:" << s.age <<"  " << "分數:" << s.score << endl;
}
int main()
{
    struct Student s;
    s.name = "小王";
    s.age = 20;
    s.score = 99;

    printStudent1(s);
    //printStudent2(&s);
    cout << "main函式中列印 姓名:" << s.name <<"  " << "年齡:" << s.age <<"  " << "分數:" << s.score << endl;
    
    system("pause");
    return 0;
}

列印結果如下:

值傳遞中,形參發生任何的改變(

)不修飾實參(

),即實參不會發生任何改變。

地址傳遞: 

void printStudent2(struct Student * p)
{
    p->age = 100;
    cout << "子函式中通過地址傳遞列印 姓名:" << p->name << "  " << "年齡:" << p->age << "  " << "分數:" << p->score << endl;
}
int main()
{
//將學生傳入到一個引數中,列印學生身上的所有資訊
    struct Student s;
    s.name = "小王";
    s.age = 20;
    s.score = 99;

    //printStudent1(s);
    printStudent2(&s);
    cout << "main函式中列印 姓名:" << s.name <<"  " << "年齡:" << s.age <<"  " << "分數:" << s.score << endl;
    
    system("pause");
    return 0;
}

列印結果為

地址傳遞形參(

)會修飾實參(

);即形參改變,實參也改變。

結構體中const使用場景

作用:用const來防止誤操作

例項:

#include<iostream>
#include<string>
using namespace std;

//const使用場景

struct Student
{
    string name;    //姓名
    int age;        //年齡
    int score;        //分數
};

//void printStudents(Student s)  //值傳遞會把形參的所有值(本例中的姓名、年齡、分數)複製一份,再傳給實參,這樣導致記憶體開銷太大
void printStudents(const Student *s)   //將函式中的形參改為指標,可以減少記憶體空間的使用,減小記憶體開銷,而不會像值傳遞一樣複製一個新的副本出來     //加入const 就不能修改引數值了,只能讀取 避免以後因為程式碼量太大而不小心誤操作改值
{
    //s.age = 150;
    //cout << "姓名:" << s.name << "  " << "年齡:" << s.age << "  " << "分數:" << s.score << endl;

    // s->age = 150;   //加入const 就不能修改了,只能讀取 避免以後因為程式碼量太大而不小心誤操作改值
    cout << "姓名:" << s->name << "  " << "年齡:" << s->age << "  " << "分數:" << s->score << endl;
}
int main()
{
//建立結構體變數
    struct Student s = { "張三",20,90 };

    //通過函式列印結構體變數資訊
    //printStudents(s);    //值傳遞記憶體開銷太大
    printStudents(&s);     //採用地址傳遞,每次只佔用4個位元組(指標記憶體大小為4位元組),減小記憶體開銷
    
    cout << "張三的年齡為:" << s.age << endl;

    system("pause");
    return 0;
}