位置:首頁 > 高級語言 > C++教學 > C++數據結構

C++數據結構

C/C++數組允許定義,結合同類型的多個數據項,但結構是另一個用戶定義的數據類型,它允許結合不同種類的數據項變量。

結構是用來代表一個記錄,假設想跟蹤圖書館中的書籍。可能要跟蹤有關每本書以下屬性:

  • Title(標題)
  • Author(作者)
  • Subject(科目)
  • Book ID(編號)

定義一個結構:

定義一個結構,必須使用結構的語句。該結構語句定義了一個新的數據類型,具有多個成員,程序結構語句的格式應該是這樣的:

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

結構變量是可選的,每個成員的定義是一個正常的變量定義,如int i; or float f; 或任何其他有效的變量的定義。在該結構的定義的結尾,最後的分號之前,可以指定一個或多個結構變量,但它是可選的。這裡是聲明圖書的結構方式:

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

現在,可以存儲結構變量的地址在上麵定義的指針變量。為了找到一個結構變量的地址,&操作符結構的名字前,如下所示:

struct_yiibaier = &Book1;

使用一個指向結構訪問結構的成員,必須使用- >操作如下:

struct_yiibaier->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 yiibaier 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 關鍵字

有一個簡單的方法來定義結構,或者可以使用“alias”類型來創建。例如:

typedef struct
{
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
}Books;

現在,可以使用上麵書籍,無需使用struct關鍵字來定義圖書類型變量。下麵是一個例子:

Books Book1, Book2;

可以使用typedef關鍵字用於非結構以及如下:

typedef long int *pint32;
 
pint32 x, y, z;

x,y和z是所有的指針到長整型