경쟁 프로그래밍에서 효율적으로 C/C++ 코드 작성
우선 당신이 알아야 할 것은 주형 매크로 그리고 벡터 다음 단계로 넘어가기 전에!
- 템플릿은 특정 유형에 독립적인 방식으로 코드를 작성하는 일반 프로그래밍의 기초입니다.
- 매크로는 이름이 지정된 코드 조각입니다. 이름이 사용될 때마다 매크로의 내용으로 대체됩니다.
- 벡터는 요소가 삽입되거나 삭제될 때 자동으로 크기를 조정하고 해당 저장소가 컨테이너에 의해 자동으로 처리되는 기능을 갖춘 동적 배열과 동일합니다.
따라서 우리는 효과적인 방법으로 코드를 작성하기 위해 이러한 강력한 도구를 사용할 수 있습니다.
경쟁 프로그래밍에 사용할 수 있는 몇 가지 멋진 트릭은 다음과 같습니다.
// C++ program to demonstrate range based for // loops for accessing vector and array elements #include #include using namespace std ; int main () { // Create a vector object that // contains 5 elements vector < int > vec = { 0 1 2 3 4 }; // Type inference by reference using auto. // Range based loops are preferred when no // modification is needed in value for ( const auto & value : vec ) cout < < value < < ' ' ; cout < < 'n' ; // Basic 5 element integer array int array [] = { 1 2 3 4 5 }; for ( const auto & value : array ) cout < < value < < ' ' ; return 0 ; }
산출:
0 1 2 3 4 1 2 3 4 5
#include template < typename T > void printList ( std :: initializer_list < T > text ) { for ( const auto & value : text ) std :: cout < < value < < ' ' ; } // Driver program int main () { // Initialization list printList ( { 'One' 'Two' 'Three' } ); return 0 ; }
산출:
One Two Three
#include // Call by reference is used in x template < typename T typename U > static inline void amin ( T & x U y ) { if ( y < x ) x = y ; } // call by reference is used in x template < typename T typename U > static inline void amax ( T & x U y ) { if ( x < y ) x = y ; } // Driver program to find the Maximum and Minimum value int main () { int max_val = 0 min_val = 1e5 ; int array [] = { 4 -5 6 -9 2 11 }; for ( auto const & val : array ) // Same as max_val = max (max_val val) // Same as min_val = min (min_valval) amax ( max_val val ) amin ( min_val val ); std :: cout < < 'Max value = ' < < max_val < < ' n ' < < 'Min value = ' < < min_val ; return 0 ; }
산출:
Max value = 11 Min value = -9
#include template < typename T > void scan ( T & x ) { x = 0 ; bool neg = 0 ; register T c = getchar (); if ( c == '-' ) neg = 1 c = getchar (); while (( c < 48 ) || ( c > 57 )) c = getchar (); for ( ; c < 48 || c > 57 ; c = getchar ()); for ( ; c > 47 && c < 58 ; c = getchar () ) x = ( x < < 3 ) + ( x < < 1 ) + ( c & 15 ); if ( neg ) x *= -1 ; } template < typename T > void print ( T n ) { bool neg = 0 ; if ( n < 0 ) n *= -1 neg = 1 ; char snum [ 65 ]; int i = 0 ; do { snum [ i ++ ] = n % 10 + '0' ; n /= 10 ; } while ( n ); -- i ; if ( neg ) putchar ( '-' ); while ( i >= 0 ) putchar ( snum [ i -- ]); putchar ( 'n' ); } // Driver Program int main () { int value ; // Taking input scan ( value ); // Printing output print ( value ); return 0 ; }
Input: 756 Output: 756
빠른 입력 및 출력에 대해 자세히 알아보려면 이 기사를 읽어보세요 .
#include using namespace std ; #define rep(in) for (i = 0; i < n; ++i) #define REP(ikn) for (i = k; i <= n; ++i) #define REPR(ikn) for (i = k; i >= n; --i) // Driver program to test above Macros int main () { int i ; int array [] = { 4 5 6 9 22 11 }; int size = sizeof ( array ) / sizeof ( array [ 0 ]); // Default 0 index based loop rep ( i size ) cout < < array [ i ] < < ' ' ; cout < < ' n ' ; // Starting index based loop REP ( i 1 size -1 ) cout < < array [ i ] < < ' ' ; cout < < ' n ' ; // Reverse for loop REPR ( i size -1 0 ) cout < < array [ i ] < < ' ' ; return 0 ; }
산출
4 5 6 9 22 11 5 6 9 22 11 11 22 9 6 5 4
std::ios_base::sync_with_stdio(false);
궁극적으로 이러한 현명한 트릭을 사용하면 최소한의 시간과 단어로 코드를 쉽게 작성할 수 있습니다.