vector的capacity增長方式

2021-04-02 15:42:41 字數 2988 閱讀 9037

vector的capacity增長方式

vector的capacity()呼叫返回vector中最大能夠儲存的元素個數,也即在下一次需要擴充容量之前能容納的元素個數。reserve會使容器在必要的時候增長,以便容納制指定數目的元素。

例程:

#include

#include

using namespace std;

int main(int argc, char *argv)

return 0; }

上面的**在

dev-c++和

vs2005中分別列印如下的結果:

dev-c++ 5

10

initial capaciry of u: 0

now capacity is :1

now capacity is :2

now capacity is :4

now capacity is :4

now capacity is :8

now capacity is :8

now capacity is :8

now capacity is :8

now capacity is :16

now capacity is :16

now capacity is :16

now capacity is :16

now capacity is :16

now capacity is :16

now capacity is :16

now capacity is :16

now capacity is :32

now capacity is :32

now capacity is :32

now capacity is :32

now capacity is :32

now capacity is :32

now capacity is :32

now capacity is :32

now capacity is :32

now capacity is :32

now capacity is :32

now capacity is :32

now capacity is :32

now capacity is :32

now capacity is :32

now capacity is :32

now capacity is :64

now capacity is :64

now capacity is :64

now capacity is :64

now capacity is :64

now capacity is :64

now capacity is :64

now capacity is :64

vs20055

10

initial capaciry of u: 0

now capacity is :1

now capacity is :2

now capacity is :3

now capacity is :4

now capacity is :6

now capacity is :6

now capacity is :9

now capacity is :9

now capacity is :9

now capacity is :13

now capacity is :13

now capacity is :13

now capacity is :13

now capacity is :19

now capacity is :19

now capacity is :19

now capacity is :19

now capacity is :19

now capacity is :19

now capacity is :28

now capacity is :28

now capacity is :28

now capacity is :28

now capacity is :28

now capacity is :28

now capacity is :28

now capacity is :28

now capacity is :28

now capacity is :42

now capacity is :42

now capacity is :42

now capacity is :42

now capacity is :42

now capacity is :42

now capacity is :42

now capacity is :42

now capacity is :42

now capacity is :42

now capacity is :42

now capacity is :42

從執行結果看到,對於reserve呼叫(包括resize),兩個編譯器都是將capacity擴大到恰好容納指定的元素。exceptional c++ style第一條中說對reserve的呼叫產生的容量可能大於指定的值,這應該是因為標準只規定了至少要滿足指定的值,至於具體的值則由實現自己決定。

但對於普通的新增元素而導致的增長,兩者的方式就不一樣了。

dev-c++(

gcc)採用的是

2^n這麼個公式,即按

1,2,4,8,16,32,64這樣的規律增長;而

vs2005應該也是按某種規律增長,具體公式沒看出來!

vs2005的方式在某些情況下可能稍微節省一點記憶體,但相應的可能會需要更多次地擴充記憶體,也許效率稍差一點。

Vector 中size和 capacity的區別

size是指容器當前擁有元素的個數,而capacity是指容器在必須分配新的儲存空間之前可以存放的元素總數。如vectorivect 10 ivect.capacity 10,ivect.size 0,當你向ivect中插入元素時,只要沒有超過十個,那麼capacity就不變,而size為你插入的元...

Yarn的fair和capacity排程器

yarn通過可插拔方式提供排程策略,由於fifo對多使用者情況欠缺,這裡只介紹容量排程 capacity 和公平排程器 fair 相同點 1.支援多佇列多使用者,提高叢集資源利用率 2.支援動態載入配置檔案,比如修改了配置,yarn rmadmin refreshqueues即可重新整理 3.支援資...

STL容器中size和capacity的區別

size 表示當前容器中元素的個數 capacity 表示當前容器在必須分配新的儲存空間之前允許存放元素的個數 與size對應的函式是resize n 它的作用是調整容器的大小使其能夠容納n個元素,如果n小於當前容器的size,則刪除多餘的元素。如果n大於當前容器的size,則新增初始化後的元素。r...

C 中vector和vector的區別

在使用stl容器 比如map list vector等 的時候,是用放一個物件還是放一個物件指標,即是用vector還是vector,這裡的vector可以換成其他的容器,int可以換成其他基本型別,也可以自定義的資料結構或類。首先,要說明的是,這兩種方式,怎麼用都可以實現功能,把一組整型數放到容器...

vector 比 vector慢的原因和替代方法

vector不是容器,至少,不是標準意義上的容器。vector的名稱有點讓人誤解,因為其內部元素實際上並不是標準的bool值,標準的bool值至少與char擁有一樣的大小,從而可以被 正常 使用。然而c 標準對於vector值有其特殊的實現方法。目的是為了減小空間的耗用。特殊版本內部只使用一個bit...