簡體   English   中英

為什么std :: vector的構造函數接口是用C ++ 11改變的?

[英]Why was the constructor interface of std::vector changed with C++11?

為什么使用新標准刪除了默認參數? 我經常構造一個這樣的矢量變量: std::vector<my_pod_struct> buf(100) 我想我會用C ++ 11編譯器得到編譯器錯誤。

explicit vector( size_type count,
                 const T& value = T(),                   /* until C++11 */
                 const Allocator& alloc = Allocator());
         vector( size_type count,
                 const T& value,                         /* since C++11 */
                 const Allocator& alloc = Allocator());

以前,當你寫std::vector<T> buf(100); 您將獲得一個T默認構造,然后該實例將被復制到向量中的一百個插槽。

現在,當你寫std::vector<T> buf(100); ,它將使用另一個構造函數: explicit vector( size_type count ); 這將默認構造一百T s。 這是一個微小的差異,但重要的一點。

新的單參數構造函數不要求類型T可以復制。 這很重要,因為現在的類型可以移動而不是可復制的。

你不會,你的用例現在有單獨的構造函數:

explicit vector(size_type n);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM