그냥 게임개발자

auto 타입 본문

C++ 나만의 복습

auto 타입

sudoju 2024. 4. 4. 22:59

auto는 C#의 var와 같다.

타입 추론을 하여 결정되는 타입이다.

#include <iostream>
using namespace std;

int a = 1;
auto b = 1;
int main()
{
    cout << b << '\n';
}

 

다음 코드처럼 b라는 변수를 auto로 선언했다.

1인 것을 rvalue가 1인 것을 통해 자동적으로 int  타입의 변수를 선언했듯이 쓸 수 있는 것을 볼 수가 있다.

 

auto는 보통 주로 복잡하거나 타입이름이 긴 것들을 대신해서 쓸때가 있다.

 

tuple<int, int, int> == auto

 

이렇게 선언이 될수도 있는 것이다.

#include <iostream>

using namespace std;

int main()
{
    vector<pair<int, int>> v;
    for (int i = 1; i <= 5; ++i)
    	v.push_back({i, i});
        
    for (auto it : v)
    	cout << it.first << " : " << it.second << '\n';
    
    for (pair<int, int> it : v)
    	cout << it.first << " : " << it.second << '\n';
        
    return 0;
}

 

끄읕

'C++ 나만의 복습' 카테고리의 다른 글

역참조 연산자  (0) 2024.04.05
포인터  (1) 2024.04.05
pair와 tuple  (0) 2024.04.04
float, double  (0) 2024.04.04
long long  (0) 2024.04.04