Cplusplus-cpp-data-structures
C ++データ構造
C/C ++配列を使用すると、同じ種類の複数のデータ項目を結合する変数を定義できますが、 structure は別の種類のデータ項目を結合できるユーザー定義のデータ型です。
構造はレコードを表すために使用されます。図書館で本を追跡したいとします。 あなたは、各本に関する次の属性を追跡することができます-
- タイトル
- 著者
- 件名
- ブックID
構造の定義
構造を定義するには、structステートメントを使用する必要があります。 structステートメントは、プログラムの複数のメンバーを持つ新しいデータ型を定義します。 構造体ステートメントの形式はこれです-
struct [structure tag] {
member definition;
member definition;
...
member definition;
} [one or more structure variables];
- 構造タグ*はオプションであり、各メンバー定義はint iなどの通常の変数定義です。またはfloat f;または他の有効な変数定義。 構造の定義の最後で、最後のセミコロンの前に、1つ以上の構造変数を指定できますが、これはオプションです。 ここにあなたが本の構造を宣言する方法があります-
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
} book;
構造体メンバーへのアクセス
構造体のメンバーにアクセスするには、* memberアクセス演算子(。)を使用します。 メンバーアクセス演算子は、構造変数名とアクセスする構造メンバーの間のピリオドとしてコーディングされます。 *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_を直接使用して、structキーワードを使用せずに_Books_タイプの変数を定義できます。 以下は例です-
Books Book1, Book2;
次のように非構造体に typedef キーワードを使用できます-
typedef long int *pint32;
pint32 x, y, z;
x、y、zはすべてlong intへのポインターです。