Latest web development tutorials

strutture di dati C ++

C / C ++ consente di definire la variabile di array per memorizzare lo stesso tipo di elementi di dati, ma lastruttura è C ++, un altro tipo di dati definiti dall'utente disponibili, che consente di memorizzare diversi tipi di elementi di dati.

Struttura utilizzato per rappresentare un record, si supponga di voler monitorare libri della biblioteca dinamica, potrebbe essere necessario tenere traccia di ogni libro le seguenti proprietà:

  • titolo
  • autore
  • soggetto
  • libro ID

struttura definizione

Al fine di definire la struttura, è necessario utilizzare l'istruzionestruct.dichiarazione struct definisce un nuovo tipo di dati che contiene una pluralità di elementi, l'istruzione di formato struct è il seguente:

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

tag struttura è facoltativo, ogni definizione membro è la definizione standard di una variabile, come ad esempio int i; o float f; o altre definizioni valide variabili.Alla fine della struttura definita, l'ultima prima del punto e virgola, è possibile specificare una o più variabili strutturali, che è facoltativo. Ecco la struttura di dichiarazione Prenota entro:

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

L'accesso a membro della struttura

Per accedere ai membri della struttura, si usal'operatore di accesso utente (.).operatore di accesso Utente è una struttura d'epoca nomi e le strutture dei nostri membri variabile vogliono accedere mezzo. È possibile definire il tipo di una struttura variabile utilizzandola parola chiave struct.L'esempio seguente illustra l'uso della struttura:

#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;
}

Quando il codice di cui sopra è compilato ed eseguito, produce i seguenti risultati:

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

Struttura come parametro funzione

È possibile inserire la struttura come un parametro di funzione, il modo Senato simile ad altri tipi di variabili o puntatori. È possibile utilizzare l'esempio precedente il modo di accedere alle variabili di struttura:

#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;
}

Quando il codice di cui sopra è compilato ed eseguito, produce i seguenti risultati:

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

Puntatore a una struttura

È possibile definire un puntatore alla struttura, il modo in cui la definizione di legami con altri tipi simili di variabile puntatore, come segue:

struct Books *struct_pointer;

Ora è possibile indirizzare le variabili struttura di stoccaggio nella definizione di cui sopra di una variabile puntatore. Per trovare l'indirizzo della variabile struttura, l'operatore & davanti al nome della struttura, come segue:

struct_pointer = &Book1;

Per utilizzare il puntatore per accedere ai punti di struttura membro alla struttura, è necessario utilizzare l'operatore ->, come segue:

struct_pointer->title;

Usiamo il puntatore struttura di riscrivere l'esempio precedente, che vi aiuterà a capire il concetto di puntatori struttura:

#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;
}

Quando il codice di cui sopra è compilato ed eseguito, produce i seguenti risultati:

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

parola chiave typedef

Ecco un modo semplice per definire la struttura, è possibile creare il tipo di prendere un "Alias". Ad esempio:

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

Ora è possibile utilizzare per definire le variabili tipiLibriLibri, senza la necessità di utilizzare la parola chiave struct. I seguenti sono esempi:

Books Book1, Book2;

È possibile utilizzare la parola chiavetypedef per definire tipo non strutturali, come segue:

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

x, yez siano rivolte a un puntatore long int intero lungo.