C++ の typedef
C++ の typedef キーワードは、既存のデータ型、ユーザー定義のデータ型、およびより意味のある名前へのポインターにエイリアスを付けるために使用されます。 Typedef を使用すると、標準データ型にわかりやすい名前を付けることができ、コードを自己文書化するのにも役立ちます。ほとんどの場合、typedef はエイリアシングに使用されますが、事前定義された名前が長すぎるか複雑すぎて何度も書き込むことができない場合にのみ使用されます。 typedef を不必要に使用することは、一般に良い習慣ではありません。
構文:
typedef
例:
typedef std::vector vInt;
以下は typedef を実装するための C++ プログラムです。
C++
// C++ Program to implement typedef> #include> > using> namespace> std;> > int> main()> {> > // Now we can make more vectors by using vInt> > typedef> std::vector <> int> >vInt;>> > > // vec1 is a vectorof type int> > vInt v;> > > v.push_back(190);> > v.push_back(180);> > v.push_back(10);> > v.push_back(10);> > v.push_back(27);> > > for> (> auto> X : v) {> > cout < < X < <> ' '> ;> > }> > > return> 0;> }> |
190 180 10 10 27
C++ での typedef のアプリケーション
- C++ の typedef は、事前定義されたデータ型に長い名前を付けるために使用できます。
- 次のような STL データ構造で使用できます。 ベクター、文字列、マップなど
- typedef は配列でも使用できます。
- typedef を使用できます 通常のポインタ 同様に 関数ポインタ 。
事前定義されたデータ型での typedef の使用
Typedef は、次のような事前定義されたデータ型のエイリアスに使用できます。 int、char、float、 およびその派生製品のようなもの 長いもの、短いもの、署名されたもの、 そして 無署名。 新しいエイリアスは、それぞれの型の新しい変数を作成するために使用できます。
構文:
typedef
例:
C++
// C++ for using typedef with predefined data types> #include> > using> namespace> std;> > int> main()> {> > // ulli can now be used for making more> > // unsigned long long int type variables> > typedef> unsigned> long> long> int> ulli;> > // ulli used to make variables> > ulli a{ 1232133 };> > cout < < a;> > return> 0;> }> |
出力
1232133
STL データ構造での typedef の使用
typedef は一緒に使用することもできます STL データ構造、 のように ベクター、文字列、マップなど 私たちが、全体をインポートしたくない人であれば、 標準名前空間 コードでは、std::vector、std::string などを何度も記述する必要があります。したがって、この場合、typedef を使用すると、これを防止し、コードをクリーンで読みやすい状態に保つ簡単な方法となります。
構文:
typedef
例:
C++
// C++ Program to display usage for typedef with vectors> #include> #include> > int> main()> {> > // Now we can make more vectors by using vInt> > typedef> std::vector <> int> >vInt;>> > // vec1 is a vectorof type int> > vInt vec1{ 1, 2, 3, 6, 2, 1 };> > > // Outputting the vector> > for> (> int> i = 0; i std::cout < < vec1[i] < <' '; } return 0; }> |
出力
1 2 3 6 2 1
配列での typedef の使用
typedef は、(STL データ構造で使用するのと同じように) 新しい配列を作成するために配列とともに使用できます。コードを読みやすくシームレスに保ちながら、typedef と配列を使用して、新しい配列を簡単に作成したり、配列の配列を作成したりできます。
構文:
typedef [ ]
この後は < エイリアス名> < 型の配列の作成に使用できるようになりました。 データの種類> そしてサイズ 。
C++
// C++ program to show use of typedef with arrays> #include> using> namespace> std;> > int> main()> {> > > typedef> int> arr[3];> > > // Making new 1D array> > > arr array1{ 1 , 1, 1};> > > > cout < <> 'Array output: '> > < <> '
'> ;> > for> (> int> i = 0; i <3; i++) {> > cout < < array1[i] < <> ' '> ;> > }> > cout < <> '
'> ;> > > // Making new 2D array> > // Matrix is an array of arrays with size> > // ( 3 X 3 )> > arr matrix[3];> > > cout < <> 'Matrix output: '> > < <> '
'> ;> > > for> (> int> i = 0; i <3; i++) {> > for> (> int> j = 0; j <3; j++) {> > // Initializing the matrix> > matrix[i][j] = i * j;> > }> > }> > > // Outputting the matrix> > > for> (> int> i = 0; i <3; i++) {> > for> (> int> j = 0; j <3; j++) {> > cout < < matrix[i][j] < <> ' '> ;> > }> > cout < <> '
'> ;> > }> > > return> 0;> }> |
出力
Array output: 1 1 1 Matrix output: 0 0 0 0 1 2 0 2 4
ポインターを使用した typedef の使用
Typedef はポインターでも使用できます。ポインターの作成を高速化し、コードを読みやすく保つため。これらは、データ ポインターと関数ポインターの両方で使用できます。
( i ) データ ポインターを使用した使用法:
以下は、データ ポインターで typedef を使用するための構文、例、およびソース コードです。
構文:
typedef *
例:
typedef int* iPtr; iPtr pointer1, pointer2;
以下は、データ ポインタで typedef を使用するプログラムです。
C++
// C++ Program to showcase the use of typedef> // with data pointer> > #include> using> namespace> std;> > int> main()> {> > int> a = 10;> > int> b = 20;> > // iPtr can now be used to create new pointers of type> > // int> > typedef> int> * iPtr;> > > iPtr pointer_to_a = &a;> > iPtr pointer_to_b = &b;> > > cout < <> 'a is: '> < < *pointer_to_a < <> '
'> ;> > cout < <> 'b is: '> < < *pointer_to_b < <> '
'> ;> > > return> 0;> }> |
出力
a is: 10 b is: 20
( ii ) 関数ポインタを使用した使用法:
以下は、関数ポインターを使用した typedef の使用法を表示する構文、例、およびコードです。
構文:
typedef < return_type>(* < alias_name>)( < parameter_type>、 < parameter_type>、 .... );>>例:
#include>>// Normal pointer to a function>int>(*func_ptr1)(>int>,>int>);>>// Using typedef with pointer to a function>typedef>int>(*func_ptr2)(>int>,>int>);>>// Function to multiply two numbers>int>product(>int>u,>int>v) {>return>u * v; }>>int>main(>void>)>{>>func_ptr1 = &product;>>>// Using typedefed function pointer for creating new>>// function pointer 'new_func'>>func_ptr2 new_func_ptr = &product;>>>// Using normal pointer to a function>>int>x2 = (*func_ptr1)(3, 2);>>>// Using the new function pointer>>int>x1 = (*new_func_ptr)(2, 4);>>>std::cout < < x1 < < std::endl;>>std::cout < < x2 < < std::endl;>}>
出力
8 6ここで、func_ptr1 は通常の関数ポインタですが、func_ptr2 は typedef 関数ポインタであり、2 つの整数を引数として受け取り、戻り値の型が int である関数ポインタをさらに作成するために使用できます。
注記: func_ptr2 は独立した関数ポインターとして使用できなくなり、int を返し 2 つの int 型をパラメーターとして受け取る関数のみを指すことができる新しい関数ポインターの作成にのみ使用できます。