2021年5月6日 星期四

讀書心得 - C++ Primer (5th Edition) - Chapter 3 (1) - Namespace

Namespace - using declaration

為了讀取 stdin,程式碼會是 std::cin。 :: 的左邊是告訴 compiler 去哪個 scope 找右邊的 operand。這有時會造成程式碼過於冗長,所以有了 using declaration 讓你更簡單的呼叫 namespace 底下的成員。
    #include <iostream>
    using std::cin;
    int main()
    {
        int i;
        cin >> i;       // ok: cin is a synonym for std::cin
        cout << i;      // error: no using declaration; we must use the full name
        std::cout << i; // ok: explicitly use cout from namepsace std
        return 0;
    }
using declaration 通常不會出現在 header 裡,因為 header 會被複製到各個程式中,會導致所有 include 該 header 的程式用相同的 using declaration 容易造成命名衝突。

Library - string

  • Initialize
        #include <string>
        using std::string;
        string s1;            // default initialization; s1 is the empty string
        string s2 = s1;       // s2 is a copy of  s1
        string s3 = "hiya";   // s3 is a copy of the string literal
        string s4(10, 'c');   // s4 is cccccccccc
    
  • std::cin interaction
    cin 會因為 space 而被分開,以下程式碼若輸入 "Hello World!",則 s1 = "Hello", s2 = "World!"
        string s1, s2;
        cin >> s1 >> s2; // read first input into s1, second into s2
        cout << s1 << s2 << endl; // write both strings
    
    可以用 getline 來讀完整的 "Hello World!"
        string line;
        // read input a line at a time until end-of-file
        while (getline(cin, line))
            cout << line << endl;
        return 0;
    
  • Comparing & Adding Two string
    string 可以使用 ==, != 做比較,也可以用 + 來做字串串接
        string s1 = "hello, ", s2 = "world\n";
        string s3 = s1 + s2;   // s3 is hello, world\n
        s1 += s2;   // equivalent to s1 = s1 + s2
    
  • Iterates through the chars in a string
    可以用 for(auto c : str),也可以用 for(auto &c : str)。&c 是 reference 所以可以改變 str 裡的 char。 
        string str("some string");
        // print the characters in str one character to a line
        for (auto c : str)      // for every char in str
            cout << c << endl;  // print the current character followed by a newline
    

Related Posts:

  • 讀書心得 - 大腦衝浪 - 第十六講 第十六講 對的時間,做對的事 說實在這很難,我通常晚上狀態最好,所以沉住氣不要回家通常都會很充實。但到了家發現耍廢的時間不夠,影響到睡眠 => 隔天 => 早上 => 晚上又好了... 我想要先拓展對的時間,所以還是制定個早睡早起的習慣… Read More
  • 讀書心得 - 大腦衝浪 - 第二十一講第二十一講 正向幻想 我想應該是像叔叔一樣的生活吧,靠著投資為主本業為輔的生活,我想當個打打程式且不被薪水跟進度摧殘的程式設計師,百分之百的享受寫程式帶來的成就感而不因是主要收入來源而討厭。 想著清晨  6:30 起床,吃著早餐看本書到 7:30,運動到 9:00 並回家洗澡,10:00上班 ~ 17:00 下班吃飯,參加著某個社團活動到 21:00 回家,23:00 就寢。 六日在家的話,同樣行程到 10:00 便睡到中午吃中… Read More
  • 讀書心得 - 大腦衝浪 - 第十一講第十一講 騰出生活的空間 我想我有一狗票東西得丟了,舊的不去新的不來。 留東西前先問自己三遍,愛它嗎 ? 能幹嘛 ? 確定嗎 ? 目標 : 一輩不會再打開的書 沒開過的海報 沒穿過的鞋子 掛到發霉的衣服 擺到發霉的褲子 抽屜裡的廢物 當兵的雜物 沒用的箱子 沒用的袋子… Read More
  • 讀書心得 - 大腦衝浪 - 第二十三講第二十三講 障礙預報 我想睡眠是最大的障礙預報,曾設過鬧鐘規定時間去睡覺但一點效都沒,我的潛意識裡認為晚上10點還早,但其實過了 10 才在做一天收尾已經算晚了,所以設定晚上 9:30。 但懶惰的狀況總是發生,我想要折衷,出發是必須即使只有 1 %的 CP 值,因為 1 > 0,1 還可以累加。… Read More
  • 讀書心得 - 大腦衝浪 - 第十三講第十三講 尤利西斯合約 尤利西斯在打贏特洛伊戰爭後,在返鄉的路上發現自己會經過 Siren 海妖的地盤,傳說海妖幽美的歌聲會迷惑水手,並讓其自行觸礁沈船。尤利西斯很想聽看看那歌聲,所以命令水手把他綁在甲板的桅杆上,並讓自己的手下用蠟堵住耳朵,拼命划不要理他的叫喊。 我想簽下未來的強制合約似乎有點用,目前最想改的是起床划手機的惡習。… Read More

0 意見:

張貼留言

Popular Posts