swap

ยทC++/๊ธฐํƒ€
ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ํ•˜๋‹ค ๋ณด๋ฉด 2๊ฐœ์˜ ๋ณ€์ˆ˜์— ๋Œ€ํ•ด์„œ ์„œ๋กœ์˜ ๊ฐ’์„ ๊ตํ™˜ํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. swap ํ•จ์ˆ˜๋ฅผ ์“ฐ๋ฉด ํŽธํ•˜์ง€๋งŒ, ์ง์ ‘ ํšจ๊ณผ๋ฅผ ๊ตฌํ˜„ํ•ด์•ผ ํ•  ๋•Œ๋„ (ํ˜น์‹œ?) ์žˆ์„ ๊ฒƒ ์ž…๋‹ˆ๋‹ค. ๊ฐ€๋ น a = 40, b = 30์ผ ๋•Œ a = 30, b = 40์œผ๋กœ ๋งŒ๋“œ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ €๋งŒ ๊ทธ๋Ÿฐ์ง€ ๋ชจ๋ฅด๊ณ˜์ง€๋งŒ, ํ•ญ์ƒ ๋ณด์กฐ ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•ด์„œ ์ด ๋ฐฉ๋ฒ•์„ ํ•ด๊ฒฐํ–ˆ์—ˆ์Šต๋‹ˆ๋‹ค. int n ๊ฐ™์€ ๋ณ€์ˆ˜์— a ๋‚˜ b ์ค‘ ํ•˜๋‚˜๋ฅผ ์ €์žฅํ•ด ๋†“๊ณ , ์ˆœ์ฐจ์ ์œผ๋กœ ๋ฐ”๊พธ๋Š” ๊ฒƒ์ด์ฃ . int a {40}; int b {30}; int n {}; n = a; a = b; b = n; cout
Chan Lee
'swap' ํƒœ๊ทธ์˜ ๊ธ€ ๋ชฉ๋ก