天天看點

c++資料結構

作者:原在路上路途陽光

C / C++ 數組允許您定義組合了同一種類的多個資料項的變量,

但結構是另一種使用者定義的資料類型,它允許您組合不同種類的資料項。

結構用于表示記錄,假設您想要在圖書館中跟蹤您的書籍。 您可能想要跟蹤每本書的以下屬性 :

  • Title - 标題
  • Author - 作者
  • Subject - 學科
  • Book ID - 圖書ID

定義結構

要定義一個結構,你必須使用struct語句。 結構語句為你的程式定義了一個新的資料類型,并且有多個成員。

結構語句的格式是這樣的 :

struct [structure tag] {
   member definition;
   member definition;
   ...
   member definition;
} [one or more structure variables];  
           

結構标記是可選的,每個成員定義是一個普通的變量定義,比如int i; 或浮動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;        // Declare Book1 of type Book
   struct Books Book2;        // Declare Book2 of type Book
 
   // book 1 specification
   strcpy( Book1.title, "Learn C++ Programming");
   strcpy( Book1.author, "Chand Miyan"); 
   strcpy( Book1.subject, "C++ Programming");
   Book1.book_id = 6495407;

   // book 2 specification
   strcpy( Book2.title, "Telecom Billing");
   strcpy( Book2.author, "Yakit Singha");
   strcpy( Book2.subject, "Telecom");
   Book2.book_id = 6495700;
 
   // Print Book1 info
   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;

   // Print Book2 info
   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;        // Declare Book1 of type Book
   struct Books Book2;        // Declare Book2 of type Book
 
   // book 1 specification
   strcpy( Book1.title, "Learn C++ Programming");
   strcpy( Book1.author, "Chand Miyan"); 
   strcpy( Book1.subject, "C++ Programming");
   Book1.book_id = 6495407;

   // book 2 specification
   strcpy( Book2.title, "Telecom Billing");
   strcpy( Book2.author, "Yakit Singha");
   strcpy( Book2.subject, "Telecom");
   Book2.book_id = 6495700;
 
   // Print Book1 info
   printBook( Book1 );

   // Print Book2 info
   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;        // Declare Book1 of type Book
   struct Books Book2;        // Declare Book2 of type Book
 
   // Book 1 specification
   strcpy( Book1.title, "Learn C++ Programming");
   strcpy( Book1.author, "Chand Miyan"); 
   strcpy( Book1.subject, "C++ Programming");
   Book1.book_id = 6495407;

   // Book 2 specification
   strcpy( Book2.title, "Telecom Billing");
   strcpy( Book2.author, "Yakit Singha");
   strcpy( Book2.subject, "Telecom");
   Book2.book_id = 6495700;
 
   // Print Book1 info, passing address of structure
   printBook( &Book1 );

   // Print Book1 info, passing address of structure
   printBook( &Book2 );

   return 0;
}

// This function accept pointer to structure as parameter.
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都是指向長整數的指針。

繼續閱讀