C++/ํ•จ์ˆ˜ (Function)

int max_num = *std::max_element(vec.begin(), vec.end()); int min_num = *std::min_element(vec.begin(), vec.end()); auto max_num_index = (std::find(vec.begin(), vec.end(), max_num) - vec.begin()); auto min_num_index = (std::find(vec.begin(), vec.end(), min_num) - vec.begin());
c++ type string์— ๋Œ€ํ•œ getline ํ•จ์ˆ˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•œ๋‹ค. std::string mystr; std::getline(std::cin, mystr); std::cout
ํ—ค๋”์— ์†ํ•œ sort ํ•จ์ˆ˜๋Š” ๋ฐฐ์—ด ํ˜น์€ ๋ฒกํ„ฐ๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค. syntax๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. // ๋ฐฐ์—ด std::sort(arr, arr + arr_length); // ๋ฒกํ„ฐ std::sort(vec.begin(), vec.end()); ๋ฐฐ์—ด์˜ ๊ฒฝ์šฐ์—๋Š” (๋ฐฐ์—ด, ๋ฐฐ์—ด + ๋ฐฐ์—ด๊ธธ์ด) ์˜ ํ˜•ํƒœ์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๊ธธ์ด๊ฐ€ 5์ธ ๋ฐฐ์—ด my_arr์— ๋Œ€ํ•ด์„œ๋Š” std::sort(my_arr, my_arr + 5); ์ž…๋‹ˆ๋‹ค. ๋ฒกํ„ฐ์˜ ๊ฒฝ์šฐ์—๋Š” ํ•ญ์ƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
transform ํ•จ์ˆ˜๋Š” ํ—ค๋”์— ๋“ค์–ด์žˆ๋Š” ํ•จ์ˆ˜๋กœ, ๋ฒกํ„ฐ, ๋ฐฐ์—ด, ๋ฌธ์ž์—ด๊ณผ ๊ฐ™์€ iterate๋“ค์— ๋Œ€ํ•ด์„œ ๋ฒ”์œ„๋ฅผ ์ง€์ •ํ•˜์—ฌ ํ•ด๋‹น ๋ฒ”์œ„ ๋‚ด์˜ ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ์ง€์ •ํ•œ ํ•จ์ˆ˜๋ฅผ ์ผ๊ด„ ์ ์šฉ์‹œํ‚ค๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. ์ด 4๊ฐœ์˜ ์ธ์ž๋ฅผ ๋ฐ›๋Š”๋ฐ์š”, ์ˆœ์„œ๋Œ€๋กœ ์ž๋ฃŒ๋“ค์˜ ์‹œ์ž‘ ์ง€์ , ๋๋‚˜๋Š” ์ง€์ , ๋ณ€ํ™˜ํ•˜์—ฌ ๋ฐ˜ํ™˜๋ฐ›์„ ์ง€์ , ๊ทธ๋ฆฌ๊ณ  ์ ์šฉํ•  ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. string mystr = "hello everyone"; transform(mystr.begin(), mystr.end(), mystr.begin(), ::toupper); cout
Chan Lee
'C++/ํ•จ์ˆ˜ (Function)' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๊ธ€ ๋ชฉ๋ก