免費開始練習
hce_nsysu 114年 計算機概論與程式設計

第 8 題

Which of the following best describes the behavior of std::vector?
  • A The size of a std::vector is always equal to its capacity.
  • B std::vector can only store objects that have a default constructor.
  • C The push_back function always has $O(1)$ time complexity.
  • D The reserve() function changes the vector's size.
  • E The std::vector container provides contiguous storage, making it compatible with C-style arrays.

思路引導 VIP

若你需要將一個容器內的所有資料傳遞給一個只接受「傳統 C 語言指標(例如 int*)」的函式,你會希望這些資料在記憶體中的排列方式具備什麼樣的物理特性?

🤖
AI 詳解 AI 專屬家教

連續記憶體配置的特性

同學能精準選出 (E) 選項,代表你對 C++ 標準模板庫(STL)中 std::vector 的底層記憶體模型有非常紮實的理解。連續儲存(Contiguous storage)std::vector 最核心的物理特性,這意味著所有元素在記憶體中是緊密相連的,因此我們能利用指標算術(Pointer arithmetic)來快速存取,這也是為什麼它能與傳統 C 語言陣列無縫接軌(例如透過 &v[0]v.data() 取得首位址)。

動態陣列的術語辨析與難度評估

▼ 還有更多解析內容

🏷️ 相關主題

基礎資料結構原理與演算法效能分析
查看更多「計算機概論與程式設計」的主題分類考古題