千慮一得齋OnLine_觀死書齋-Yahoo/Hexun/Blogger/sina/Xuite

觀死書齋暨Spread、和訊博客全文檢索

2019年9月30日 星期一

C++自修入門實境秀、C++ Primer 5版研讀秀 54/ ~ 9.2.4. Defining and Initializing a Co...





頁336

List Initialization

串列初始化

7:40

Sequential Container Size-Related Constructors

循序容器與大小相關的建構器

原文並無「與」!

和循序容器(sequential container)大小相關的建構器(constructor)

18:00

只帶一個容器大小引數的建構器,只對循序容器有效:

Note: The constructors that take a size are valid only for sequential containers; they are not supported for the associative containers.

對關聯式容器(associative container)無效

Library arrays Have Fixed Size

程式庫型別的array容器,有固定的大小

Just as the size of a built-in array is part of its type, the size of a library array is part of its type.

大小就是array型別的一部分,不管是是不是內建的型別

array<int, 42> // type is: array that holds 42 int s

array<string, 10> // type is: array that holds 10 string s

原來人這樣定義程式庫型別的array(library array)

To use an array type we must specify both the element type and the size:

要用array就一定要指定元素型別和元素多少(即容器大小)

array<int>::size_type j; // error: array<int> is not a type

只有元素型別是不足以構成一個array型別的



25:50

頁337

31:55

用串列初始化(list initialization)array其元素數量(即用來初始化元素的初始器數量)最多只能和定義的array一樣多:

If we list initialize the array , the number of the initializers must be equal to or less than the size of the array .

可見預設初始化和值初始化(value initialize)

的差別僅在有沒有提供初始器,若沒有,才是預設初始化;若有,則提供的不夠的,才會做值初始化(value initialize)

If there are fewer initializers than the size of the array , the initializers are used for the first elements and any remaining elements are value initialized (§ 3.3.1 , p. 98 ).

不管是要用預設初始化或值初始化,只要元素是類別型別(class type),那該類別必定要提供預設建構器(default constructor)來初始化其元素才行(才能進行值初始化):

In both cases, if the element type is a class type, the class must have a default constructor in order to permit value initialization:

array<int, 10> ia1; // ten default-initialized ints

array<int, 10> ia2 = {0,1,2,3,4,5,6,7,8,9}; // list initialization

array<int, 10> ia3 = {42}; // ia3[0] is 42, remaining elements are 0

這樣,預設初始化完全不提供初始器、串列初始化(list initialization)和值初始化有提供初始器,但不夠用(value initialize)就很清楚了

47:00

It is worth noting that although we cannot copy or assign objects of built-in array types (§ 3.5.1 , p. 114 ), there is no such restriction on array :

int digs[10] = {0,1,2,3,4,5,6,7,8,9};

int cpy[10] = digs; // error: no copy or assignment for built-in

array<int, 10> digits = {0,1,2,3,4,5,6,7,8,9};

array<int, 10> copy = digits; // ok: so long as array types match

可見內建型別的array不會出現array這個關鍵字

練習9.11

55:10

#include<vector>

using namespace std;



int main() {

vector<int> veci;

//Every container type defines a default constructor (§ 7.1.4, p. 263).

//With the exception of array, the default constructor creates an empty container of the specified type.

/*Again excepting array, the other constructors take arguments that

specify the size of the container and initial values for the elements.*/

vector<int>veci1(3,2);

vector<int>veci2(3);

/*There are two ways to create a new container as a copy of another one:

We can directly copy the container:*/

vector<int>veci3= veci1;//容器拷貝時,對拷雙方的容器型別與元素型別必須一致

vector<int>veci31(veci1);//the constructor that takes a container to copy

// or (excepting array) we can copy a range of elements denoted by a pair of iterators.:

vector<int>veci4(veci2.begin(), veci2.end());

vector<int>veci5 = {1,1,1};

vector<int>veci6(4,4);

veci6 = { 1,1,1 };//size會改變成串列的,而不是不足的用值初始化!veci6.size=3,veci6.capacity()=4;

//好像只有array因為是固定大小,串列初始化(list initialization)不夠的剩下才會用值初始化

}

1:24:09

練習9.12

一個是容器對拷,一個是元素對拷

因為迭代器指向的是元素,故是元素對拷

因為是元素對拷,所以只要元素型別有著轉型關係,就可以了。至於對拷雙方容器是什麼,是不相干的

練習9.13

1:42:30

#include<vector>

#include<list>

using namespace std;



int main() {

vector<int> veci{-1,-2,-3,0};

list<int> lsti{0,1,2,3};

vector<double> vecdi(veci.cbegin(),veci.cend());

vector<double> vecdl(lsti.cbegin(), lsti.cend());

}



因為容器型別與元素型別有所不同,所以要用元素拷貝的方式來初始化,也就是用帶有二個迭代器作為引數的建構器來初始化

沒有留言:

張貼留言