向量:調(diào)整大小()與std:載體:Reserve()中的注釋部分中有一個(gè)線程。這個(gè)職位關(guān)于使用std::vector::reserve()v.V.std::vector::resize().這是原始代碼:void MyClass::my_method(){
my_member.reserve(n_dim);
for(int k = 0 ; k < n_dim ; k++ )
my_member[k] = k ;}我相信在vector,正確的做法是打電話std::vector::resize(),不是std::vector::reserve().實(shí)際上,在VS 2010 SP1中調(diào)試構(gòu)建中的以下測(cè)試代碼“崩潰”:#include <vector>using namespace std;int main(){
vector<int> v;
v.reserve(10);
v[5] = 2;
return 0;}我是對(duì)的還是我錯(cuò)了?VS2010SP1是對(duì)的,還是錯(cuò)的?
3 回答

呼如林
TA貢獻(xiàn)1798條經(jīng)驗(yàn) 獲得超3個(gè)贊
std::vector::reserve
std::vector::resize
- 3 回答
- 0 關(guān)注
- 396 瀏覽
添加回答
舉報(bào)
0/150
提交
取消