C++ 數據結構
C/C++數組允許定義可存儲相同類型數據項的變量,但是結構是C++中另一種用戶自定義的可用的數據類型,它允許您存儲不同類型的數據項。
結構用於表示一條記錄,假設您想要跟踪圖書館中書本的動態,您可能需要跟踪每本書的下列屬性:
- Title
- Author
- Subject
- Book ID
定義結構
為了定義結構,您必須使用struct語句。 struct 語句定義了一個包含多個成員的新的數據類型,struct 語句的格式如下:
struct [structure tag] { member definition; member definition; ... member definition; } [one or more structure variables];
structure tag是可選的,每個member definition是標準的變量定義,比如int i;或者float f;或者其他有效的變量定義。在結構定義的末尾,最後一個分號之前,您可以指定一個或多個結構變量,這是可選的。 下面是聲明Book 結構的方式:
struct Books { char title[50]; char author[50]; char subject[100]; int book_id; }book;
訪問結構成員
為了訪問結構的成員,我們使用成員訪問運算符(.) 。 成員訪問運算符是結構變量名稱和我們要訪問的結構成員之間的一個句號。 您可以使用struct關鍵字來定義結構類型的變量。 下面的實例演示了結構的用法:
#include <iostream> #include <cstring> using namespace std; struct Books { char title[50]; char author[50]; char subject[100]; int book_id; }; int main( ) { struct Books Book1; // 声明 Book1,类型为 Book struct Books Book2; // 声明 Book2,类型为 Book // Book1 详述 strcpy( Book1.title, "Learn C++ Programming"); strcpy( Book1.author, "Chand Miyan"); strcpy( Book1.subject, "C++ Programming"); Book1.book_id = 6495407; // Book2 详述 strcpy( Book2.title, "Telecom Billing"); strcpy( Book2.author, "Yakit Singha"); strcpy( Book2.subject, "Telecom"); Book2.book_id = 6495700; // 输出 Book1 信息 cout << "Book 1 title : " << Book1.title <<endl; cout << "Book 1 author : " << Book1.author <<endl; cout << "Book 1 subject : " << Book1.subject <<endl; cout << "Book 1 id : " << Book1.book_id <<endl; // 输出 Book2 信息 cout << "Book 2 title : " << Book2.title <<endl; cout << "Book 2 author : " << Book2.author <<endl; cout << "Book 2 subject : " << Book2.subject <<endl; cout << "Book 2 id : " << Book2.book_id <<endl; return 0; }
當上面的代碼被編譯和執行時,它會產生下列結果:
Book 1 title : Learn C++ Programming Book 1 author : Chand Miyan Book 1 subject : C++ Programming Book 1 id : 6495407 Book 2 title : Telecom Billing Book 2 author : Yakit Singha Book 2 subject : Telecom Book 2 id : 6495700
結構作為函數參數
您可以把結構作為函數參數,傳參方式與其他類型的變量或指針類似。 您可以使用上面實例中的方式來訪問結構變量:
#include <iostream> #include <cstring> using namespace std; void printBook( struct Books book ); struct Books { char title[50]; char author[50]; char subject[100]; int book_id; }; int main( ) { struct Books Book1; // 声明 Book1,类型为 Book struct Books Book2; // 声明 Book2,类型为 Book // Book1 详述 strcpy( Book1.title, "Learn C++ Programming"); strcpy( Book1.author, "Chand Miyan"); strcpy( Book1.subject, "C++ Programming"); Book1.book_id = 6495407; // Book2 详述 strcpy( Book2.title, "Telecom Billing"); strcpy( Book2.author, "Yakit Singha"); strcpy( Book2.subject, "Telecom"); Book2.book_id = 6495700; // 输出 Book1 信息 printBook( Book1 ); // 输出 Book2 信息 printBook( Book2 ); return 0; } void printBook( struct Books book ) { cout << "Book title : " << book.title <<endl; cout << "Book author : " << book.author <<endl; cout << "Book subject : " << book.subject <<endl; cout << "Book id : " << book.book_id <<endl; }
當上面的代碼被編譯和執行時,它會產生下列結果:
Book title : Learn C++ Programming Book author : Chand Miyan Book subject : C++ Programming Book id : 6495407 Book title : Telecom Billing Book author : Yakit Singha Book subject : Telecom Book id : 6495700
指向結構的指針
您可以定義指向結構的指針,方式與定義指向其他類型變量的指針相似,如下所示:
struct Books *struct_pointer;
現在,您可以在上述定義的指針變量中存儲結構變量的地址。 為了查找結構變量的地址,請把& 運算符放在結構名稱的前面,如下所示:
struct_pointer = &Book1;
為了使用指向該結構的指針訪問結構的成員,您必須使用-> 運算符,如下所示:
struct_pointer->title;
讓我們使用結構指針來重寫上面的實例,這將有助於您理解結構指針的概念:
#include <iostream> #include <cstring> using namespace std; void printBook( struct Books *book ); struct Books { char title[50]; char author[50]; char subject[100]; int book_id; }; int main( ) { struct Books Book1; // 声明 Book1,类型为 Book struct Books Book2; // 声明 Book2,类型为 Book */ // Book1 详述 strcpy( Book1.title, "Learn C++ Programming"); strcpy( Book1.author, "Chand Miyan"); strcpy( Book1.subject, "C++ Programming"); Book1.book_id = 6495407; // Book2 详述 strcpy( Book2.title, "Telecom Billing"); strcpy( Book2.author, "Yakit Singha"); strcpy( Book2.subject, "Telecom"); Book2.book_id = 6495700; // 通过传 Book1 的地址来输出 Book1 信息 printBook( &Book1 ); // 通过传 Book2 的地址来输出 Book2 信息 printBook( &Book2 ); return 0; } // 该函数以结构指针作为参数 void printBook( struct Books *book ) { cout << "Book title : " << book->title <<endl; cout << "Book author : " << book->author <<endl; cout << "Book subject : " << book->subject <<endl; cout << "Book id : " << book->book_id <<endl; }
當上面的代碼被編譯和執行時,它會產生下列結果:
Book title : Learn C++ Programming Book author : Chand Miyan Book subject : C++ Programming Book id : 6495407 Book title : Telecom Billing Book author : Yakit Singha Book subject : Telecom Book id : 6495700
typedef 關鍵字
下面是一種更簡單的定義結構的方式,您可以為創建的類型取一個"別名"。 例如:
typedef struct { char title[50]; char author[50]; char subject[100]; int book_id; }Books;
現在,您可以直接使用Books來定義Books類型的變量,而不需要使用struct關鍵字。 下面是實例:
Books Book1, Book2;
您可以使用typedef關鍵字來定義非結構類型,如下所示:
typedef long int *pint32; pint32 x, y, z;
x, y 和z 都是指向長整型long int 的指針。