[問題] copy&vector的問題

看板C_and_CPP (C/C++)作者 (嘿嘿嘿)時間15年前 (2011/01/20 02:35), 編輯推噓0(003)
留言3則, 1人參與, 最新討論串1/1
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) Dev-C++ 問題(Question): vector<int> c(3,1); vector<int> y; y.reserve(3); copy(c.begin(),c.end(),y.begin()); for(int i=0;i<3;i++) { cout<<c[i]<<" "<<y[i]<<endl; } 這樣可以得到 1 1 1 1 1 1 如果把黃色地方改成 vector<int> c; c.reserve(5); for(int i=0;i<=4;i++) { c[i]=1; } 這樣結果會變成 1 4984576 1 4997892 1 0 小弟不才 不了解其中的差異 上來釐清觀念 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 114.46.85.71

01/20 03:19, , 1F
vector::reserve ? vector::resize? 傻傻分不清楚
01/20 03:19, 1F

01/20 03:20, , 2F

01/20 03:20, , 3F
兩段程式碼都寫錯, 看一下說明再叫用吧...
01/20 03:20, 3F
文章代碼(AID): #1DDoxlMH (C_and_CPP)
文章代碼(AID): #1DDoxlMH (C_and_CPP)