C++自修入門實境秀、C++ Primer 5版研讀秀 30/ ~5.4.3. Range for Statement-20190816_171000





練習5.11

6:40

{

char a;

int aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0;

int tCnt = 0, bCnt = 0, nCnt = 0;

string s{""};

while (getline(cin,s))

{

s = s + '\n';//參考中文版頁87,getline是不包括newline的

string::iterator siter{s.begin()};

while (siter != s.end())

{

switch (*siter)

{

case '\t':

++tCnt;

break;

case '\n':

++nCnt;

break;

case ' ':

++bCnt;

break;

case 'A':

case 'a':

++aCnt;

break;

case 'E':

case'e':

++eCnt;

break;

case 'I':

case'i':

++iCnt;

break;

case 'O':

case'o':

++oCnt;

break;

case'u':

++uCnt ;

break;

default:

break;

}

++siter;

}

}

cout << aCnt << '\n'

<< eCnt << '\n'

<< iCnt << '\n'

<< oCnt << '\n'

<< uCnt << '\n'

<< "blank space:"<<bCnt << '\n'

<<"tab:" <<tCnt << '\n'

<<"new line:" <<nCnt << '\n'

<< endl;

}

練習5.12

15:59

{

char a;

int ffCnt = 0,flCnt=0,fiCnt=0;

int m = 0;

while (cin>>a) {

if (m==1)

{

if (a=='f' || a=='i' || a=='l')

{

switch (a)

{

case 'f':

++ffCnt;

m = 0;

break;

case 'l':

++flCnt;

m = 0;

break;

case 'i':

++fiCnt;

m = 0;

break;

default:

break;

}

}

else//if NOT (a=='f' || a=='i' || a=='l')

m = 0;

}

else//if(m=0)

if (a=='f')

++m;

}



cout << "ff:"<< ffCnt << '\n'

<< "fl:" << flCnt << '\n'

<< "fi:" << fiCnt << '\n'

<< endl;

}

43:40

練習5.13

Code for Exercise 5.13 46:00 頁184

47:37

//(a)

unsigned aCnt = 0, eCnt = 0, iouCnt = 0;

char ch = next_text(3);

switch (ch) {

case 'a': aCnt++;

break;//原來少了break; statement!

case 'e': eCnt++;

break;

default: iouCnt++;

break;

}

printf("%i\n", aCnt);

printf("%i\n", eCnt);

printf("%i\n", iouCnt);

(b)有兩種解法:

//(b)

vector<int> ivec{ 2,3,4 };

unsigned index = some_value();

switch (index) {



case 1:

{int ix = get_value();

ivec[ix] = index;

break; }

default:

int ix = ivec.size() - 1;

ivec[ix] = index;

break;//原缺break;

}

59:20

//(b)

vector<int> ivec{ 2,3,4 };

unsigned index = some_value();

int ix = get_value();

switch (index) {

case 1:

ivec[ix] = index;

break;

default:

ix = ivec.size() - 1;

ivec[ix] = index;

break;//原缺break;

}

1:2:10

1:7:50

//(c)

unsigned evenCnt = 0, oddCnt = 0;

int digit = get_num() % 10;

switch (digit) {

//case 1, 3, 5, 7, 9://這釋是VBA的寫法,但VBA又沒有末綴的「:」

case 1:

case 3:

case 5:

case 7:

case 9:

++oddCnt;//原C誤作小寫,下同

break;

case 2:

case 4:

case 6:

case 8:

case 10:

++evenCnt;

break;

default:

break;

}

//(d)

const unsigned ival = 512, jval = 1024, kval = 4096;//加上const就好了

unsigned bufsize;

unsigned swt = get_bufCnt();

switch (swt) {

case ival://標籤值必須是常值運算式(constant expression)

bufsize = ival * sizeof(int);

break;

case jval:

bufsize = jval * sizeof(int);

break;

case kval:

bufsize = kval * sizeof(int);

break;

default:

break;

}

3:30如果Visual Studio 2019端的版本還是舊的,就可以用sync中的pull或sync來與網路上GitHub的版本同步。pull應該就是將外部的版本拉進來同步的意思。

1:13:20

5.4. Iterative Statements 5.4迭代式的述句

即「迴圈」也!

就是while和for這兩種(乃至range for)

The while and for statements

此可稱條件式迭代(iterate)

1:17:00

還有do while這種是在執行一次後才測試條件,看看是否還要再幹下去

The do while executes the body and then tests its condition.

5.4.1. The while Statement

If the first evaluation of condition yields false , statement is not executed.

如果第一次評估條件式就是false的話,那while裡面的述句就不會被執行。

看我翻的比較像中文,還是中文版翻的?!

∴ 中文能力真的很重要!光外文能力好是不足的!



The condition can be an expression or an initialized variable declaration (§ 5.2 , p. 174 ).

其中的條件(condition)可以是一個運算式或一個初始化的變數宣告(§5.2)。

具備初始設定式(initializer)的變數宣告。



Variables defined in a while condition or while body are created and destroyed on each iteration.

1:29:40

Using a while Loop

頁185

1:33:00

1:37:10

練習5.4

2:25:00

{

string word; unsigned cnt = 0; unsigned cntMax = 0; string s, wordMost{""};

vector<string> svec;



while (cin>>s)

{

svec.push_back(s);

}

vector<string>::iterator sIter=svec.begin();

while (sIter !=svec.end())

{

word = *(sIter++);

if (sIter == svec.end()) break;

if (word==*sIter)

{

++cnt;

}

else

{

if (cnt>cntMax)

{

cntMax= cnt;

wordMost = word;

}

cnt = 0;

}

}

if (cntMax==0)

{

cout << "沒有重複的字" << endl;

}

else

{

cout << "重複最多的字:" << wordMost << '\t'

<< "重複次數:" << cntMax

<< "總次數:" << cntMax+1

<< endl;

}





}

5.4.2. Traditional for Statement

傳統的for即條件式的for,非range for

頁186

Execution Flow in a Traditional for Loop

2:32:20

2:40:00

visibility 和生命週期應是同義的

2:41:35

Multiple Definitions in the for Header

2:47:30

Omitting Parts of the for Header

A for header can omit any (or all) of init-statement , condition , or expression .

for的標頭竟然全部都可省略。但「;」分號不可!

頁187

Omitting condition is equivalent to writing true as the condition.

省略了for的條件式就是直接用 true來設定那個條件式

for (int i = 0; /* 沒有條件 */ ; ++i) {

//處理i,迴圈内的程式碼必須停止迭代!

}

停止迭代就是用break;或return;述句。



We can also omit expression from the for header. In such loops, either the condition or the body must do something to advance the iteration.

advance 這裡翻成「推進」

所以不一定是主體那裡在迭代,而在條件式中也可。

vector<int> v;

for (int i; cin >> i; /* no expression */ )

v.push_back(i);

In this loop there is no need for an expression because the condition changes the value of i. The condition tests the input stream so that the loop ends when we’ve read all the input or encounter an input error.

3:3:55

5.4.3. Range for Statement

The new standard introduced a simpler for statement that can be used to iterate through the elements of a container or other sequence.

所以range for 是新標準才引進的

頁188

3:22:10

In a range for , the value of end() is cached. If we add elements to (or remove them from) the sequence, the value of end might be invalidated (§ 3.4.1 , p. 110 ). We’ll have more to say about these matters in § 9.3.6 (p. 353 ).

在一個範圍for中,end()的值 是有快取(cached)的。



練習5.15

//(a)

int ix = 0; int sz = 0;

for (; ix != sz; ++ix) { /* . . . */ }

if (ix == sz)

// . . .

// (b)

int ix=0, sz=0;

for (; ix != sz; ++ix) { /* . . . */ }

//for的標頭只有中間二個分號,末尾沒有分號

//(c)

int sz = 2;

for (int ix = 0; ix != sz; ++ix, ++sz)

{ /* . . . */ }//除了一開始ix==sz,否則同時道士遞增,二者將永無相同之時,此便成了無窮迴圈

3:37:40

練習5.16

3:40:59

練習5.17

bool isVector_prefix(vector<int>vec1,vector<int>vec2) {

unsigned i = 0;

for (;i!=vec1.size();++i)

{

if (vec1[i]==vec2[i])

{



}

else

{

break;

}

}

if (i==vec1.size())

{

return true;

}

else

{

return false;

}

}

void test() {

vector<int>pvec{ 0, 1, 1, 2 }, vec{ 0, 1, 1, 2, 3, 5,8 };

cout<<isVector_prefix(pvec,vec)<<endl;



}

留言

熱門文章