C++自修入門實境秀、C++ Primer 5版研讀秀 26/ ~sizeof comma Operator - Visual Studio 2...





Visual Studio 2019 with GitHub

15:40

What's new in Visual Studio 2019

4:30

Cloud-first workflow

22:00 What's New in Visual Studio 2019 – Collaborate



27:40

Getting Started with GitHub using Visual Studio 2019

Getting Started Faster with Git and GitHub

對檔案做的更動要切換到指定branch下才能被該branch 記錄

https://youtu.be/9ITVx4VvkcI?t=684

1:2:00

所以 commit message is required! 有了這個才能 pull request

1:8:40

push 推 ②單字想複詞 推出(發行、面世),推出(新產品)的推

推出去了才做拉請求(請引用、採納)

推,也是 ②單字想複詞 推薦的推。推薦我改的方案,請求人採用引(拉)進

1:19:20

approve the change

merge the change,要到GitHub才行

GitHub Extension for Visual Studio https://visualstudio.github.com/

Pull Requests 提出接受(採納、納入pull)的請求(拉請求)

翻譯 要講究語意與語境,不能只翻字!

拉 ②單字想複詞 拉我一把(應翻成「援」)、拉進去圈子……的「拉」(pull)

10:00

Open the GitHub pane by typing GitHub into Visual Studio Quick Launch (Ctrl+Q).



GitHub Training & Guides

https://www.youtube.com/githubguides

Git達人教你搞懂GitHub基礎觀念

GitHub 的 Fork 是什么意思?

30 天精通 Git 版本控管 (28):了解 GitHub 的 fork 與 pull request 版控流程

1:34:10

在Visual Studio 2019使用 GitHub基本流程:

1.創建分支 branch(未到分支目錄,可以refresh,就可看到)

2.儲存變更,然後查看changes

3.commit message and push

4.approve(應是需要不同使用者才有此功能)

5 merge(在GitHub上 4:17:50應該不必。4:22:20真的還是要到網站)

6.若檔案未更新,則要在team explorer 中的branch選取要更新的branck,按下滑鼠右鍵,點選「 checkout」(目前在第一項)

2:25:00

記得都要按F5鍵刷新(或功能表中的「 」圖示)

checkout後或重新命名branch後都不會立即更新,須要刷新refresh(F5)才行

checkout意思就是看該分支(branch)現在的情形

一般都是在設定後自己會刷新,但Team Explorer不會,所以要注意在操作完任何步驟後,都要按下「F5」鍵才能看到最update的情況(學校個人雲沒問題,是我這台虛擬電腦安裝的才這樣吧!)



4.9. The sizeof Operator

傳回一個型別或運算式基於位元組的大小值

右結合(right associative)

傳回的是size_t型別的常值運算式(constant expression)

頁157

5:10:20 sizeof不會估算其運算元 所以它的運算元可以是未初始化的變數

sizeof(type) sizeof(array)

2:58:20 5:8:05 type→type name

sizeof語法有二種形式(forms)

1.有括號的,則對型別運算。——型別名稱為其運算元

2.無括號的,則對運算式運算——運算式名稱(或變數名稱)為其運算元

sizeof data; // size of data 's type, i.e., sizeof(Sales_data)

sizeof p; // size of a pointer

sizeof *p; // size of the type to which p points, i.e., sizeof(Sales_data)

sizeof data.revenue; // size of the type of Sales_data's revenue member

sizeof Sales_data::revenue; // alternative way to get the size of revenue

後兩者是不同的,末者是revenue本身的size

如果revenue是double型別,52.3 會較5.23 size大嗎?結果是一樣的:

double s = 52.3, t = 5.23;

size_t szs = sizeof s, szt = sizeof t, szd{ sizeof(double) };

if (szs==szt)

{

if (szs== szd)

{

cout << szs << '\t' << szt << endl;



}

}

The result of applying sizeof depends in part on the type involved:

• sizeof char or an expression of type char is guaranteed to be 1.

1個位元組是8位元。char是8位元。

sizeof an array is the size of the entire array. It is equivalent to taking the sizeof the element type times the number of elements in the array. Note that sizeof does not convert the array to a pointer.

⑦先抓動詞 5:40:00。因為sizeof不會估算運算元,所以陣列當作運算元時仍被看作是陣列。也就是說,陣列只有在被估算時才會被估為指標(pointer)。

就是因為sizeof a array 是該陣列元素型別的大小和其元素個數的乘積,所以sizeof(array)/sizeof(*array)=陣列的大小(dimension=size() =元素個數)

Because sizeof returns the size of the entire array, we can determine the number of elements in an array by dividing the array size by the element size:



• sizeof a string or a vector returns only the size of the fixed part of these types; it does not return the size used by the object’s elements.

sizeof—個string或vector只會回傳這些型別固定部分的大小,不會回傳物件元素所佔用的大小。

是否是因為string 和vector都是可變大小的,而可變大小,即其元素數量,故不會傳回其元素所佔用的大小,而只會就其固定不變的部分,傳回其大小。可見sizeof取得是常值的本性。都是有固定大小特性的。如array的size也是固定的,作為其型別的一部分,在宣告定義時就須指定也。



6:11:00 複合型別(compound type)

6:8:30

在 C++ 裡,struct 與class的唯一區別是預設的存取等級,class是私有的,struct 則是公有的。https://zh.wikipedia.org/wiki/%E8%A4%87%E5%90%88%E5%9E%8B%E5%88%A5

之前以為struct和class的差別在於有沒有方法成員(即成員函式)看來並非如此。

5:59:20

頁158

練習4.28

6:2:39

6:17:50

Fundamental (built-in) types https://docs.microsoft.com/zh-tw/cpp/cpp/cpp-type-system-modern-cpp?view=vs-2019#fundamental-built-in-types

Unlike some languages, C++ has no universal base type from which all other types are derived. The language includes many fundamental types, also known as built-in types. This includes numeric types such as int, double, long, bool, plus the char and wchar_t types for ASCII and UNICODE characters, respectively.

numeric types本書是作「arithmetic type」(頁32):

C++ defines a set of primitive types that include the arithmetic types and a special type named void. The arithmetic types represent characters, integers, boolean values,and floating-point numbers. The void type has no associated values and can be used in only a few circumstances, most commonly as the return type for functions that do not return a value.



Most fundamental types (except bool, double, wchar_t and related types) all have unsigned versions, which modify the range of values that the variable can store. For example, an int, which stores a 32-bit signed integer, can represent a value from -2,147,483,648 to 2,147,483,647. An unsigned int, which is also stored as 32-bits, can store a value from 0 to 4,294,967,295. The total number of possible values in each case is the same; only the range is different.……For a complete list of built-in types and their size and numeric limits, see Fundamental Types.

The following illustration shows the relative sizes of the built-in types:

Size in bytes of built-in types.

這就是本題的答案了。



Type Size Comment

int 4 bytes The default choice for integral values.

double 8 bytes The default choice for floating point values.

bool 1 byte Represents values that can be either true or false.

char 1 byte Use for ASCII characters in older C-style strings or std::string objects that will never have to be converted to UNICODE.

wchar_t 2 bytes Represents "wide" character values that may be encoded in UNICODE format (UTF-16 on Windows, other operating systems may differ). This is the character type that is used in strings of type std::wstring.

unsigned char 1 byte C++ has no built-in byte type. Use unsigned char to represent a byte value.

unsigned int 4 bytes Default choice for bit flags.

long long 8 bytes Represents very large integer values.



6:16:00

C語言所定義的資料型別如下

C++ 的基本資料型態

6:50:00

int i;

double d;

bool b;

char c;

wchar_t wc;

unsigned char uc;

unsigned int ui;

long long ll;

wchar_t* p;

wchar_t& r=wc;

vector<double> ivec;

string s="銾";



cout << "int:" << sizeof(i) << endl;

cout << "double:" << sizeof(d) << endl;

cout << "bool:" << sizeof(b) << endl;

cout << "char:" << sizeof(c) << endl;

cout << "wchar_t:" << sizeof(wc) << endl;

cout << "unsigned char:" << sizeof(uc) << endl;

cout << "unsigned int:" << sizeof(ui) << endl;

cout << "long long:" << sizeof(ll) << endl;

cout << "指標(pointer):" << sizeof(p) << endl;//指標的sizeof永遠是8

cout << "參考(reference):" << sizeof(r) << endl;//參考的sizeof看它參考的本尊是什麼型別而定

cout << "vector:" << sizeof(ivec) << endl;//vector的sizeof永遠是32

cout << "string:" << sizeof(s) << endl;//string的sizeof永遠是40

練習4.29

int x[10];

int* p = x;

cout << sizeof(x) / sizeof(*x) << endl;

cout << sizeof(p) / sizeof(*p) << endl;

「10」 and 「2」//指標的sizeof永遠是8,int是4,故8/4=2

練習4.30

(a) sizeof x+y

(b) sizeof p->mem[i]

(c) sizeof a<b

(d) sizeof f(); //只有(a)、(c)要加括號

7:22:00

Visual Studio 2019

7:26:40 按住ctrl再按滑鼠左鍵可以跳至定義查看

Ctrl+d 在文字編輯器中,是複製上一行,在solution explorer 是加入現存檔案

4.10. Comma Operator 逗號運算子

其運算元有2,且由左至右先後被估算

7:43:11 逗號運算子也能保證估算的順序



7:54:20

練習4.31

8:22:30

結果竟然是一樣?!

vector<int>ivec{13,5,7,91};

vector<int>::size_type cnt = ivec.size();

for (vector<int>::size_type ix = 0; ix != ivec.size(); ix++, cnt--)

ivec[ix] = cnt;

練習4.32

constexpr int size = 5;

int ia[size] = { 1,2,3,4,5 };

for (int* ptr = ia, ix = 0; ix != size && ptr != ia + size; ++ix, ++ptr) {

*ptr = ix;

cout << ix << endl;

}

練習4.33

8:41:00

constexpr int size = 5;

int ia[size] = { 1,2,3,4,5 };

for (int* ptr = ia, ix = 0; ix != size && ptr != ia + size;*ptr<5 ? (++ix, ++ptr) :( --ix, --ptr)) {

*ptr = ix;

cout << ix << endl;

}

8:52:00

int ix = 3, ptr = 3;

ix % 2 ? (++ix, ++ptr) : (--ix, --ptr);

cout << ix<<'\t'<<ptr << endl;

頁159

4.11. Type Conversions

留言

熱門文章