반응형
C++11 for 문
- Java, C#의 foreach()와 유사함
- STL(C++ 표준 라이브러리)의 list, vector 등의 컨테이너 사용가능
- 사용자 정의 list도 사용가능(추가 작업 필요)
#include <iostream>
int main()
{
int x[5] = { 1,2,3,4,5 };
// C++11 for
for ( int n : x )
{
std::cout << n << std::endl;
}
// C++11 for
for ( auto n : x ) // 편하게 auto 권장
{
std::cout << n << std::endl;
}
// C++11 하위 for
for (int i = 0; i < 10; i++)
{
std::cout << n << std::endl;
}
}
C++17 if init, switch init, if constexpr
- 조건문(if, switch)에 초기화 구문을 포함 가능
- if( 초기화문; 조건문 ), switch( 초기화문; 조건문 )
- if constexpr( 컴파일 시간 조건문 ), 컴파일 시간의 상수 조건만 확인 가능, 템플릿 프로그래밍에서
#include <iostream>
int main()
{
// C++17 if
if ( int result = foo(); result == 1) { }
// C++17 switch
switch( int result = foo(); result ) { }
// C++17 if constexpr
constexpr int a = 1;
if constexpr ( a == a) { }
// 이전 if
int result = foo();
if ( result == 1 ) {}
// 이전 switch
int result = foo();
switch( result ) {}
}
반응형
'프로그래밍 언어 > C++' 카테고리의 다른 글
C++ 함수 특징 #2 (0) | 2019.05.05 |
---|---|
C++ 함수 특징 #1 (0) | 2019.05.05 |
C++ 변수의 특징(variable) #2 (0) | 2019.01.15 |
C++ 변수의 특징(variable) #1 (0) | 2019.01.14 |
C++ 표준 입출력(Basic Input/Output) (1) | 2019.01.13 |