[C++]
배열과 벡터의 복사
Hevton
2022. 10. 8. 22:29
반응형
배열
memmove(destination, source, sizeof(source));
벡터
copy(source.begin(), source.end(), destionation.begion());
-> 공간 초기화를 하지 않은 채 복사하면 에러가 생긴다. (https://notepad96.tistory.com/48)
그래서 그냥 대입연산자를 사용해도 깊은 복사가 되니까, 대입연산자를 쓴다.
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> v = {1, 2, 3, 4, 5};
vector<int> b;
b = v;
b[0] = 2;
for(int i = 0 ; i < 5; i++) {
cout << v[i] << " ";
}
cout <<'\n';
for(int i = 0 ; i < 5; i++) {
cout << b[i] << " ";
}
}
Output
1 2 3 4 5
2 2 3 4 5
반응형