logo

C++의 형식 정의

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는 다음과 같이 미리 정의된 데이터 유형의 별칭을 지정하는 데 사용될 수 있습니다. 정수, 문자, 부동 소수점, 그리고 그 파생물은 다음과 같습니다 긴 것, 짧은 것, 서명된 것, 그리고 서명되지 않은. 그런 다음 새 별칭을 사용하여 각 유형의 새 변수를 만들 수 있습니다.

통사론:

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::벡터, 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>, .... );>

예:

typedef int (*fun_ptr)(int, int); fun_ptr new_ptr = &function;>

이제 fun ptr을 사용하여 더 많은 함수 포인터를 생성할 수 있습니다. 이는 아래 코드에서 더 명확해집니다.

C++




#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를 반환하고 두 개의 int 유형을 매개 변수로 사용하는 함수만 가리킬 수 있는 새 함수 포인터를 만드는 데에만 사용할 수 있습니다.

적대적 검색