struktur data C ++
C / C ++ memungkinkan Anda untuk mendefinisikan variabel array untuk menyimpan jenis yang sama dari item data, tetapistruktur C ++, user-defined tipe data lain yang tersedia, yang memungkinkan Anda untuk menyimpan berbagai jenis item data.
Struktur yang digunakan untuk mewakili rekor, misalkan Anda ingin melacak buku-buku perpustakaan yang dinamis, Anda mungkin perlu untuk melacak setiap buku properti berikut:
- judul
- penulis
- subyek
- buku ID
struktur definisi
Dalam rangka untuk menentukan struktur, Anda harus menggunakan pernyataanstruct.Pernyataan struct mendefinisikan tipe data baru yang berisi sejumlah anggota, pernyataan Format struct adalah sebagai berikut:
struct [structure tag] { member definition; member definition; ... member definition; } [one or more structure variables];
tag struktur adalah opsional, setiap definisi anggota adalah definisi standar dari variabel, seperti int i; atau mengapung f, atau definisi variabel valid lainnya.Pada akhir struktur didefinisikan, yang terakhir sebelum titik koma, Anda dapat menentukan satu atau lebih variabel struktural, yang opsional. Berikut adalah struktur deklarasi Pesan hingga:
struct Books { char title[50]; char author[50]; char subject[100]; int book_id; }book;
Akses ke anggota struktur
Untuk mengakses anggota dari struktur, kitamenggunakan operator akses anggota (.).Operator akses anggota adalah struktur periode nama variabel dan struktur dari anggota kami ingin mengakses antara. Anda dapat menentukan jenis struktur variabelmenggunakan kata kunci struct.Contoh berikut menunjukkan penggunaan struktur:
#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; }
Ketika kode di atas dikompilasi dan dijalankan, menghasilkan hasil sebagai berikut:
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
Struktur sebagai parameter fungsi
Anda dapat menempatkan struktur sebagai parameter fungsi, cara Senat mirip dengan jenis lain dari variabel atau pointer. Anda dapat menggunakan contoh di atas jalan untuk mengakses variabel struktur:
#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; }
Ketika kode di atas dikompilasi dan dijalankan, menghasilkan hasil sebagai berikut:
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
Pointer ke struktur
Anda dapat menentukan pointer ke struktur, cara definisi link ke sejenis lainnya variabel pointer, sebagai berikut:
struct Books *struct_pointer;
Sekarang Anda dapat mengatasi variabel struktur penyimpanan dalam definisi di atas dari sebuah variabel pointer. Untuk menemukan alamat dari variabel struktur, & operator di depan nama struktur, sebagai berikut:
struct_pointer = &Book1;
Untuk menggunakan pointer untuk mengakses poin struktur anggota untuk struktur, Anda harus menggunakan -> operator, sebagai berikut:
struct_pointer->title;
Mari kita gunakan pointer struktur untuk menulis ulang contoh di atas, itu akan membantu Anda memahami konsep pointer struktur:
#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; }
Ketika kode di atas dikompilasi dan dijalankan, menghasilkan hasil sebagai berikut:
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
kata kunci typedef
Berikut adalah cara sederhana untuk menentukan struktur, Anda dapat membuat jenis untuk mengambil "Alias." Sebagai contoh:
typedef struct { char title[50]; char author[50]; char subject[100]; int book_id; }Books;
Sekarang Anda dapat menggunakan untuk mendefinisikan variabel jenisBooksBooks, tanpa perlu menggunakan kata kunci struct. Berikut ini adalah contoh:
Books Book1, Book2;
Anda dapat menggunakan kata kuncitypedef untuk menentukan jenis non-struktural, sebagai berikut:
typedef long int *pint32; pint32 x, y, z;
x, y dan z yang menunjuk ke integer panjang int panjang pointer.