728x90
C++ ์์ ์ ๊ณตํ๋ copy ๋ฌธ์ copy, copy_if ๊ฐ ์๊ณ copy_if ๋ฅผ ์ฐ๋ฉด ์กฐ๊ฑด๋ฌธ์ ์ถ๊ฐํ ์ ์๋ค.
- ํค๋
#include <algorithm>
- ํจ์
copy(์์ ์ง์ , ๋ ์ง์ , ๋ณต์ฌ๋ ๋ณ์ ์์์ง์ );
copy_if( ์์ ์ง์ , ๋ ์ง์ , ๋ณต์ฌ๋ ๋ณ์ ์์์ง์ , ์กฐ๊ฑด);
- ์์
1) 1์ฐจ์ ๋ฐฐ์ด ๋ณต์ฌ
#include <iostream>
#include <algorithm>
int main(){
int arr[4] = {1, 2, 3, 4};
int copiedArr[4] = {0, };
copy(arr, arr+4, copiedArr);
return 0;
}
2) 2์ฐจ์ ๋ฐฐ์ด ๋ณต์ฌ
#include <iostream>
#include <algorithm>
int main(){
int arr[2][4] = {{0,-1}, {0,1}, {-1,0}, {1,0}};
int copiedArr[2][4];
copy(&arr[0][0], &arr[0][0]+(2*4), &copiedArr[0][0]);
return 0;
}
3) ๋ฒกํฐ๋ณต์ฌ
copy(v.begin(), v.end(), v_copy.begin());
* ์ฐธ๊ณ ์๋ฃ: https://en.cppreference.com/w/cpp/algorithm/copy
std::copy, std::copy_if - cppreference.com
template< class InputIt, class OutputIt > OutputIt copy( InputIt first, InputIt last, OutputIt d_first ); (1) (constexpr since C++20) template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 > ForwardIt2 cop
en.cppreference.com
728x90
'๐ Coding Test Study > C++' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[C++] ๋ฒกํฐ, ๋ฐฐ์ด ํน์ ๊ฐ์ผ๋ก ์ด๊ธฐํ ํ๊ธฐ (fill ํจ์) (0) | 2025.03.07 |
---|---|
[10์ฃผ ์์ฑ C++ ์ฝ๋ฉํ ์คํธ] ๊ธฐ๋ณธ๊ฐ๋ :: ๋นํธ๋ง์คํฌ (0) | 2024.05.06 |
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); (0) | 2024.04.15 |
๋ฌธ์์ด ASCII code ๊ฐ (0) | 2024.02.17 |
[10์ฃผ ์์ฑ C++ ์ฝ๋ฉํ ์คํธ] ๋์ ํฉ, Prefix Sum (0) | 2023.08.11 |