vector의 resize 함수를 사용할 때 어떤 방식으로 작동하는지 궁금해서 찾아봤다.


resize 함수는 vector를 초기화한 뒤 사이즈를 변경해주는 것이 아니라

1) 매개변수의 값이 vector의 사이즈보다 작으면 사이즈가 매개변수 값이 될 때 까지 끝에서부터 삭제

2) 매개변수의 값이 vector의 사이즈보다 크면 vector의 기존 요소는 변경되지 않고 뒤에 요소를 추가


--------------------------------------------------------------------------------------------------------------

2) 결과

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
temp.push_back(1);
temp.push_back(2);
temp.push_back(3);
temp.push_back(4);
 
cout << "~~resize before~~" << endl;
for (int i = 0; i < temp.size(); i++) {
    cout << temp[i] << endl;
}
 
temp.resize(5);
cout << "~~resize after~~" << endl;
for (int i = 0; i < temp.size(); i++) {
    cout << temp[i] << endl;
}
cs



--------------------------------------------------------------------------------------------------------------

1) 결과

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
temp.push_back(1);
temp.push_back(2);
temp.push_back(3);
temp.push_back(4);
 
cout << "~~resize before~~" << endl;
for (int i = 0; i < temp.size(); i++) {
    cout << temp[i] << endl;
}
 
temp.resize(2);
cout << "~~resize after~~" << endl;
for (int i = 0; i < temp.size(); i++) {
    cout << temp[i] << endl;
}
cs


https://msdn.microsoft.com/ko-kr/library/wezs0zy6.aspx

'C++' 카테고리의 다른 글

[C++] switch문 내부에서 변수 선언시 오류  (0) 2016.12.12
[라이브러리] string class 함수  (0) 2016.03.01

+ Recent posts