두개의 변수를 교체하는 것으 SWAP이라고 한다.
#include <iostream>
int main()
{
int a = 5;
int b = 10;
int temp = a;
a = b;
b = temp;
return 0;
}
<aside> 💡 새로운 데이터(상태) 를 추가 할떄마다 새로운 변수 또는 배열을 계속 추가해줘야하는단점이 존재한다. 그것을 보완하기 위해서 하나 이상의 변수를 묶어 그룹화 하는 사용자 정의 자료형을 구조체 라고 한다.
</aside>
#include <iostream>
struct Player
{
int hp;
int mp;
int exp;
};
int main()
{
Player p1{ 100, 100, 0 };
// Player p1 = { 100, 100, 0 };
// . 을 사용해서 객체의 멤버 변수를 사용한다
p1.hp = 200;
std::cout << p1.hp;
// hp, mp, exp 를 기본으로 가진 객체를 여러 개 만들 수 있다.
Player p2;
Player p3;
return 0;
}
#include <iostream>
int main()
{
int vect[3][3] =
{
{1,2,3},
{4,5,6},
{7,8,9}
};
int count = 0;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if (vect[i][j] == 5)
{
count++;
}
}
}
if (count > 0)
{
std::cout << "5 is in the array" << std::endl;
}
else
{
std::cout << "5 is not in the array" << std::endl;
}
return 0;
}
함수 호출 응용
기본적으로 전달인자는 다른 지역변수, 다른공간에 선언된 변수이기 때문에 값이 전달되는 과정에 원본이아닌 값이 복사가 되어서 다른 함수로 전달이 된다.
이를 Call by Value라고 한다.
#include <iostream>
#include <ppltasks.h>
void test(int x, int y)
{
std::cout << x << y << std::endl;
}
int main()
{
int a = 5;
int b = 6;
test(a, b);
return 0;
}