C++自修入門實境秀、C++ Primer 5版研讀秀 15/ ~3.2程式庫的sting型別圓滿-20190727_154628
頁88 0:24
Comparing string s 字串比對
比對字串運算子
9:00比對字串的原則(法則、規則)
longer larger
15:00 人在做,天在看。感應道交,不可思議。只因樂於助人,認真當真,意外發現英文版原PDF電子書 感恩感恩 南無阿彌陀佛 38:10原來google圖書改了pdf的超連結,所以點擊click here to view code image就失效了。48:15 感恩真主阿拉
40:20
頁89
Assignment for string s
Adding Two string s
The compound assignment operator (+=)
複合指定運算子
54:03VirtualBox虛擬電腦音效失效時原來只要在裝置管理員中重新啟用音訊裝置即可
51:50
Adding Literals and strings
頁90 1:4:50
When we mix strings and string or character literals, at least one operand to each + operator must be of string type:
「+」運算子用在字串型別時,有一邊一定要時字串string型別,不能只是字串字面值(literal)或字元字面值。51:20
1:21:30 subexpression次運算元
55:00 string程式庫型別(library type)允許我們將字元字面值、字元字串字面值(literal)轉換成string(字串)型別
59:55發現字元與字串相「+」時並不如預期。可見不能直觀地如此相加!
47:45 append and appendix 字形結構兼音義、字形結構換部首
1:22:40字串字面值(literal)和程式庫型別(library type)string 是不同的型別!
練習3.2 1:25:00
string.getline() 一次讀取一行 1:27:44 1:27:50 getline(istream object,string variable)還忘了怎麼用嗎?!哈哈
string s;
while (getline(cin, s))
cout << s << endl;
std::cin一次讀取一個字,因為它預設就是以半形空格來間隔
while (cin>>s)
cout << s << endl;
練習3.3 1:35:40
練習3.4 1:37:10
string s1,s2;
cin >> s1 >> s2;
if (s1!=s2)
{
if (s1>s2)
{
cout << s1 << "is larger!" << endl;
}
else
{
cout << s2 << "is larger!" << endl;
}
}
1:41:33 length
string s1,s2;
cin >> s1 >> s2;
if (s1.size()!=s2.size())
{
if (s1.size()>s2.size())
{
cout << s1 << "is longer!" << endl;
}
else
{
cout << s2 << "is longer!" << endl;
}
}
練習3.5
string s1,s2,s3,s4;
cin >> s1 >> s2;
s3 = s1 + s2;
s3 = s1 + " " + s2;// 1:49:40
cout << s3<< "\n"<<s4 << endl;
return 0;
whitespace (e.g., spaces, newlines, tabs)
1:54:05
3.2.3. Dealing with the Characters in a string
頁91 1:58:18
It turns out that the best way to deal with these cases involves different language and library facilities.
這語言是人的語言,還會程式語言?1:59:10
2:3:00
要他們記得哪些程式庫名稱繼承字C,而哪些是C++獨有的。
字→自2:12:44專為C++的標頭檔名前面會再加「C」冠頭,而末後會省略副檔名「.h」,如表3.3的「cctype」2:14:00表3.3 cctype函式
isprint(c)
ispunct(c)
isspace(c)……
2:12:44
Processing Every Character? Use Range-Based for
2:21:30 2:29:15
Range for
2:22:00
for (declaration:expression)
statement
其中expression是型別代表一種序列的一個物件,
→其中expression是代表序列型別的一個物件。2:37:00
where expression is an object of a type that represents a sequence,
2:27:10 「line」到底要翻「行」還是「列」?
string s("expression");
for (auto c:s)
{
cout << c << endl;
}
這裡的auto判斷其實就是char 2:43:10
2:47:44 somewhat
string_variable.size()
頁93 2:57:20
Using a Range for to Change the Characters in a string
Remember that a reference is just another name for a given object.
參考就是某物(件、物件)的別名。2:59:10 3:1:20
given都翻成「某(個)」就可以了。3:4:30
控制變數(control variable)
3:11:00 char的唸法、發音
3:16:45
Processing Only Some Characters? 3:19:24
下標(subscript)
下標運算子([]運算子, subscript operator)接受一個string: :size_type ( §3.2.2)值,用以代表我們想要存取的字元之位置。此運算子會回傳給定位置上字元的參考。3:24:44 4:57:00是「參考」就是可以對其做更動了
下標運算子([]運算子,subscript operator)傳回的是參考
The operator returns a reference to the character at the given position. 3:25:50
以0為基始的索引值陣列
Subscripts for string s start at zero; if s is a string with at least two characters, then s[0] is the first character, s[1] is the second, and the last character is in s[s.size() - 1] .
和c#一樣都是用[],不是像VB用()
頁94 3:38:50
Using a Subscript for Iteration在迭代中使用下標(subscript)運算
3:43:10
the logical AND operator (the && operator )
C++和C#一樣,&、&&有別
3:49:30
The important part about this operator is that we are guaranteed that it evaluates its right-hand operand only if the left-hand operand is true.
只有在運算子左邊是true的前提下才會進行運算子右邊運算元的估算。
3:59:50
string s("expressionvalue");
for (decltype(s.size()) index=0;index!=s.size()&&
!isspace(s[index]);++index)
{
s[index] = toupper(s[index]);
}
頁95 4:11:00
One way to simplify code that uses subscripts is always to use a variable of type string::size_type as the subscript.4:9:30 4:12:00
用size_type(size_t)型別來作為subscript的變數型別,這樣我們就只要檢查subscript的變數值是小於size()傳回值即可
The result of using an out-of-range subscript is undefined.
subscript應翻成「內標」或「次標記」或「嵌記」,而不是「下標」 4:17:00
。這個sub是英文,「下」的主詞 ⑧找對主詞 是範圍range之「下」(即內),在此前提range「下」的意思。4:18:20亦如「川上、池上、上海」的上,不是科學語言的上,而是文學語言的上。若要用「下標」來翻sub,也是這個文學語言的用法。否則與「上下」的「下」何干?
X2 這個「2」才叫「下」標,但這裡的subscript是index值,是指在一定range內的index,與「上下」的「下」何干?
4:21:24
Using a Subscript for Random Access
用下標(subscript)作隨機的存取
4:23:55不需要逐一(in sequence)去處理所有元素,而可以隨意指定一個想要處理的對象即可
4:26:00 16進位(十六進位)
4:47:00下標(subscript)的取值原則
頁96 4:50:20
練習3.6 4:51:01 4:59:00
string s("good");
for (auto &i:s)
{
i = 'x';
}
cout << s << endl;
5:0:00一定要用參考,才能改變原值
練習3.7
auto判斷出來的就是char型別
練習3.8 5:8:00
當然 range for最好,因為預設了range,不必再加條件式判斷何時出while、for迴圈loop了
練習3.9 5:5:40
s[0] Array index out of bounds.
練習3.10 5:13:00
string s;
getline(cin,s);/*let's go to , and have funny!
lets go to and have funny
請按任意鍵繼續 . . .
*/
string r;
for (auto i :s)
{
if (!ispunct(i))
{
r += i;
}
}
cout << r << endl;
練習3.11 5:15:44
5:20:00/**/夾注號「*/」之間不能有空格
const string s("hey s");
for (auto& c : s)
//*c = 'x';/*Error(active) E0137 expression must be a modifiable lvalue*/
cout << c << endl;
Name Value Type
c 104 'h' const char &
5:25:28
留言