C++-Datenstrukturen
C/C++-Arrays ermöglichen es Ihnen, Variablen zu definieren, die mehrere Datenelemente der gleichen Art, aber Struktur kombinieren ist ein weiterer benutzerdefinierter Datentyp, mit dem Sie Datenelemente verschiedener Art kombinieren können.
Strukturen werden verwendet, um einen Datensatz darzustellen, angenommen, Sie möchten Ihre Bücher in einer Bibliothek verfolgen. Vielleicht möchten Sie die folgenden Attribute zu jedem Buch nachverfolgen −
- Titel
- Autor
- Betreff
- Buch-ID
Eine Struktur definieren
Um eine Struktur zu definieren, müssen Sie die struct-Anweisung verwenden. Die Struct-Anweisung definiert einen neuen Datentyp mit mehr als einem Member für Ihr Programm. Das Format der Struct-Anweisung ist dieses −
struct [structure tag] { member definition; member definition; ... member definition; } [one or more structure variables];
Das Struktur-Tag ist optional und jede Mitgliedsdefinition ist eine normale Variablendefinition, wie z. B. int i; oder Schwimmer f; oder jede andere gültige Variablendefinition. Am Ende der Strukturdefinition, vor dem abschließenden Semikolon, können Sie eine oder mehrere Strukturvariablen angeben, dies ist jedoch optional. So würden Sie die Book-Struktur deklarieren −
struct Books { char title[50]; char author[50]; char subject[100]; int book_id; } book;
Auf Strukturmitglieder zugreifen
Um auf ein beliebiges Mitglied einer Struktur zuzugreifen, verwenden wir den Member-Zugriffsoperator (.) . Der Elementzugriffsoperator wird als Punkt zwischen dem Strukturvariablennamen und dem Strukturelement, auf das wir zugreifen möchten, codiert. Sie würden struct verwenden Schlüsselwort zum Definieren von Variablen des Strukturtyps. Es folgt ein Beispiel zur Erläuterung der Verwendung der Struktur −
Live-Demo#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; }
Wenn der obige Code kompiliert und ausgeführt wird, erzeugt er das folgende Ergebnis −
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
Strukturen als Funktionsargumente
Sie können eine Struktur als Funktionsargument auf sehr ähnliche Weise übergeben, wie Sie jede andere Variable oder jeden anderen Zeiger übergeben. Sie würden auf Strukturvariablen auf ähnliche Weise zugreifen wie im obigen Beispiel −
Live-Demo#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; }
Wenn der obige Code kompiliert und ausgeführt wird, erzeugt er das folgende Ergebnis −
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
Zeiger auf Strukturen
Sie können Zeiger auf Strukturen auf sehr ähnliche Weise definieren, wie Sie Zeiger auf jede andere Variable wie folgt definieren:−
struct Books *struct_pointer;
Nun können Sie die Adresse einer Strukturvariablen in der oben definierten Pointer-Variablen speichern. Um die Adresse einer Strukturvariablen zu finden, setzen Sie den &-Operator wie folgt vor den Namen der Struktur −
struct_pointer = &Book1;
Um auf die Mitglieder einer Struktur mit einem Zeiger auf diese Struktur zuzugreifen, müssen Sie den -> Operator wie folgt verwenden −
struct_pointer->title;
Lassen Sie uns das obige Beispiel mit dem Strukturzeiger umschreiben, hoffen, dass Sie das Konzept leicht verstehen können −
Live-Demo#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; }
Wenn der obige Code kompiliert und ausgeführt wird, erzeugt er das folgende Ergebnis −
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
Das typedef-Schlüsselwort
Es gibt eine einfachere Möglichkeit, Strukturen zu definieren, oder Sie könnten von Ihnen erstellte Typen "aliasieren". Zum Beispiel −
typedef struct { char title[50]; char author[50]; char subject[100]; int book_id; } Books;
Jetzt können Sie Bücher verwenden direkt, um Variablen von Büchern zu definieren geben Sie ein, ohne das Schlüsselwort struct zu verwenden. Es folgt das Beispiel −
Books Book1, Book2;
Sie können typedef verwenden Schlüsselwort für Nicht-Strukturen sowie folgt −
typedef long int *pint32; pint32 x, y, z;
x, y und z sind alle Zeiger auf lange Ganzzahlen.
C Sprache