hlc23
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    --- title: C++(1) 基礎語法 tags: 簡報,C++,csc type: slide --- {%hackmd @hlc23/text_highlight %} # C++ 基礎語法 --- [About me](https://hackmd.io/@hlc23/about_me) --- #### 目錄 1. 原始碼檔案架構 2. 變數 3. 數學運算子 4. 邏輯/關係 運算子 5. 基本輸入輸出 ---- 註: 此簡報教學內容較偏向**程式競賽**用法 部份技巧在**專案開發**可能較不適用 --- ## 原始碼檔案架構 ---- ## <span class=sample>sample</span> ```cpp= #include <iostream> using namespace std; int main(){ cout<<"Hello World"; return 0; } ``` 你是否想過前兩行在做什麼? ---- 簡單分解成幾部份來說明 ---- ## <span class=pre_cmd>#include</span> ``` ``` ++include++在英文中有**包含**,**包括**的意思 在C/C++中用於引入其他已經完成的程式碼 以便於重複使用 ``` ``` 前面的 **<span class=pre_cmd>\#</span>** 記得要打 Note: 此處 # 用法不細講 ---- ## <span class=pre_cmd> &lt;iostream> </span> 是一個內建<font color=#33a9ff>標頭檔</font> 內含標準輸入(<span class=obj>cin</span>)輸出(<span class=obj>cout</span>) ``` ``` <font color=#33a9ff>標頭檔</font>(head file) <font color=#33a9ff>標頭檔</font>是一種包含C\+\+程式碼的檔案 通常用來宣告<span class=obj>變數</span>、<span class=fn>函數</span>、<span class=obj>類別</span>等程式元素 以便在其他的C\+\+檔案中使用 ---- ## 補充 在程式競賽中,為求方便 有一個++萬用標頭檔++ 引入它就等於引入好幾個常用的標頭檔 ### <span class=pre_cmd> &lt;bits/stdc++.h> </span> --- 註:此標頭檔只在GCC系列的編譯器能使用 ---- ## <span class=keyword>using namespace</span> <span class=obj>std</span>; 使用 <span class=obj>std</span> <span class=keyword>命名空間</span> ``` ``` <span class=obj>std</span>? <span class=keyword>命名空間</span>? 這些是什麼? ---- ## <span class=keyword>命名空間(namespace)</span> <span class=keyword>命名空間</span>是用來區分相同名稱的<span class=obj>物件(常數、函式)</span>等 避免誤用、混淆 ``` ``` 概念類似於 <font color=#9fffff>**台中市**</font> 的 <font color=#9fd358>**民族路**</font> <font color=#9fffff>**彰化市**</font> 的 <font color=#9fd358>**民族路**</font> 其中 <font color=#9fffff>**台中市 彰化市**</font> 就是<span class=keyword>命名空間</span>的概念 ---- ### <span class=sample>sample</span> <span class=obj>cin</span>, <span class=obj>cout</span> 其實是<span class=obj>std</span><span class=op>::</span><span class=obj>cin</span>, <span class=obj>std</span><span class=op>::</span><span class=obj>cout</span> ``` ``` 註:這裡的<span class=op>::</span>是一個運算子 用於指定特定的命名空間 如 <span class=obj>std</span><span class=op>::</span> <span class=obj>cin</span>意思是 <span class=obj>std</span> <span class=keyword>命名空間</span> 的 <span class=obj>cin</span> ---- ## <span class=obj>std</span> ``` ``` <span class=obj>std</span>是C\+\+標準庫的<span class=keyword>命名空間</span>,它包含了C\+\+語言提供的各種功能和數據結構 --- ## 變數 ---- #### 常用變數類型 - <span class=type>int</span> (<span class=type>long long</span>) - <span class=type>float</span> (<span class=type>double</span>) - <span class=type>bool</span> - <span class=type>char</span>/<span class=type>string</span> ---- ### 變數命名規則 - 只使用英文、數字、底線(_) - 不使用雙下底線(__)作為開頭 - 開頭不能為數字 - 與關鍵字不重複 ---- ## <span class=type>int</span> $-2147483648 \sim 2147483647$ $-2^{16}\sim 2^{16}-1$ ```cpp= int n = 1450; ``` ---- #### <span class=type>long long int</span> $-9223372036854775807 \sim 9223372036854775807$ $-2^{32}\sim 2^{32}-1$ ```cpp= long long n = 314159265358979323 ``` 一般只要看到數字大於$10^6$就可以考慮使用 ---- ## <span class=type>float</span> | Type Name | bits | 精準度 | |:----------------------------------- | ---- | --------------- | | <span class=type>float</span> | 32 | $6 \sim 7 位$ | | <span class=type>double</span> | 64 | $15 \sim 16 位$ | | <span class=type>long double</span> | 128 | $18\sim 19 位$ | 浮點數存在誤差,能不用就不用 ---- ### <span class=sample>sample</span> ```cpp!= float f1 = 3.1415927; double f2 = 3.141592653589793; long double f3 = 3.1415926535897932384; ``` ---- ## <span class=type>bool</span> 布林值(<span class=true>true</span> / <span class=false>false</span>)同時也能作為1跟0使用 ```cpp= bool b1 = true, b2 = false; ``` ---- ## <span class=sample>sample</span> ```cpp= signed main(){ bool b1 = true; cout<<(b1+b1)*2; // output = 4 } ``` ---- ## <span class=type>char</span> 字元 常配合陣列(<font color=#c80bfd>Array</font>)使用 或是利用ASCII編碼特性使用 ```cpp char w = 'a'; ``` ---- ## <span class=type>string</span> 字串,一個比樓上的<span class=type>char</span>還好用的東西 用來儲存字元序列 ```cpp string text = "hello world"; ``` --- ## 數學運算子 ---- #### 二元數學運算子 ``` ``` | 名稱 | 符號 | | ---- | ---- | | 加 | <span class=m_op>+</span> | | 減 | <span class=m_op>-</span> | | 乘 | <span class=m_op>\*</span> | | 除 | <span class=m_op>/</span> | | 模除(取餘) | <span class=m_op>%</span> | ---- ### <span class=sample>sample</span> ```cpp= int n1 = 2 + 3; // n1 = 5 int n2 = 5 - 2; // n2 = 3 int n3 = 6 * 2; // n3 = 12 int n4 = 8 / 4; // n4 = 2 int n5 = 7 % 5; // n5 = 2 ``` ---- ### 縮寫 ``` ``` 一個整數<font color=#c80bfd>n1</font>的值為3 ```cpp int n1 = 3; ``` 若要將<font color=#c80bfd>n1</font>加上5 可以寫 ```cpp n1 = n1 + 5; ``` 或是下面的縮寫 ```cpp n1 += 5; ``` ---- #### <span class=sample>sample</span> ```cpp int n = 2; n -= -5; // n=7 n *= 6; // n=42 n /= 5; // n=8 n %= 3; // n=2 ``` ---- #### 一元數學運算子 | 名稱 | 符號 | | ---- | ------------------------- | | 自增 | <span class=m_op>++</span> | | 自減 | <span class=m_op>-\-</span> | ---- ### 前面後面差很多 ```cpp= int a = 10; int b = ++a; // a 和 b 都是 11 int c = a++; // c 是 11,a 是 12 ``` 前: 先++改變++ 再**使用** 後: 先++使用++ 再**改變** --- ## 關係/邏輯 運算子 ---- ### 關係運算子 ``` ``` | 名稱 | 符號 | |:--------:|:-----------------------------:| | 大於 | <span class=re_op>></span> | | 小於 | <span class=re_op> &lt; </span> | | 等於 | <span class=re_op>==</span> | | 不等於 | <span class=re_op>!=</span> | | 大於等於 | <span class=re_op>>=</span> | | 小於等於 | <span class=re_op> &lt;= </span> | ---- #### 邏輯運算子 ``` ``` | 名稱 | 符號 | | ------- | -------------------------- | | 非(not) | <span class=l_op>!</span> | | 且(and) | <span class=l_op>&&</span> | | 或(or) | <span class=l_op>\|\|</span> | ---- ### not 真值表 | A | not A | |:-------------------------------- | -------------------------------- | | <span class=true>True</span> | <font color=#F6073A>False</font> | | <font color=#F6073A>False</font> | <span class=true>True</span> | ---- ### and 真值表 兩者成立則為真 | A | B | A and B | |:----- | ----- | ------- | | <span class=true>True</span> | <span class=true>True</span> | <span class=true>True</span> | | <span class=true>True</span> | <font color=#F6073A>False</font> | <font color=#F6073A>False</font> | | <font color=#F6073A>False</font> | <span class=true>True</span> | <font color=#F6073A>False</font> | | <font color=#F6073A>False</font> | <font color=#F6073A>False</font> | <font color=#F6073A>False</font> | ---- ### or 真值表 兩者中有一成立則為真 | A | B | A or B | | -------------------------------- | -------------------------------- |:-------------------------------- | | <span class=true>True</span> | <span class=true>True</span> | <span class=true>True</span> | | <span class=true>True</span> | <font color=#F6073A>False</font> | <span class=true>True</span> | | <font color=#F6073A>False</font> | <span class=true>True</span> | <span class=true>True</span> | | <font color=#F6073A>False</font> | <font color=#F6073A>False</font> | <font color=#F6073A>False</font> | ---- #### <span class=sample>sample</span> ```cpp= bool a = !True; bool b = False && False; ``` ---- <font color=#5f9ea0>關係運算子</font>與<font color=#5f9ea0>邏輯運算子</font> 運算順序為:<font color=#5f9ea0>括號</font>$\rightarrow$<font color=#5f9ea0>關係</font>$\rightarrow$<font color=#5f9ea0>邏輯</font> 運算後的結果應視為<font color=#ff6347>布林值</font> ---- #### <span class=sample>sample</span> ``` ``` 請問 b 是 <span class=true>True</span> or <span class=false>False</span>? ```cpp= bool b = !(1 == (5>4) || 3 < 2); ``` ---- ## 答案是 <span class=false>False</span> ---- ### 說明 ```cpp= bool b = !(1 == (5>4) || 3<2); = !(True == True || False) = !(True) = False ``` --- ## 基本輸入輸出 ---- 使用<span class=obj>cin</span>與<span class=obj>cout</span>進行++輸入++與++輸出++ ---- 所以具體來說怎麼用? ---- ### <span class=sample>sample</span> ```cpp= #include<iostream> using namespace std; int main(){ string text; cin>>text; cout<<text; } ``` <span class=obj>cin</span>搭配 **<span class=op>>></span>** 使用 <span class=obj>cout</span>搭配 **&lt;&lt;** 使用 ---- cin / cout用 <span class=op>>></span> 或 <span class=op>&lt;&lt;</span>傻傻分不清? ---- ### 右移(>>)與左移(&lt;&lt;) --- \>\> 是把右邊的值放到左邊的物件做使用 << 則反之 ```cpp cin>>text; cout<<"Hello World"; ``` ---- 幾個<span class=obj>cin</span>與<span class=obj>cout</span>需注意的點 ---- ### <span class=obj>cin</span> 遇到空格就會結束一個輸入 ```cpp= string a,b; cin>>a>>b; ``` 若輸入`"apple book sheep"`則 a 的值為++apple++ b 的值為++book++ 你會發現sheep沒有被記錄下來 ---- ### <span class=obj>cout</span>不會換行 如果需要換行可以使用<span class=obj>std</span><span class=op>::</span><span class=obj>endl</span>或是跳脫字元<span class=obj>&#34;\n&#34;</span> ``` ``` <span class=sample>sample</span> ```cpp cout<<"hello"<<endl<<"world"; ``` ```cpp cout<<"hello\nworld"; ``` ---- #### 補充:常用的跳脫字元 |符號|名稱| |-|-| |\\'|單引號(single quote)| |\\"|雙引號(double quote)| |\\\\ |反斜線(backslash )| |\n |換行(line feed)| |\r |回車(carriage return)| ##### 詳細請見[wiki](https://zh.m.wikipedia.org/zh-tw/%E8%BD%AC%E4%B9%89%E5%AD%97%E7%AC%A6) ---- #### 小試身手 [Zerojudge a001. 哈囉](https://zerojudge.tw/ShowProblem?problemid=a001) ---- 題目要求輸入一組文字並依格式輸出 ---- ### 觀察範例 |輸入|輸出| |-|-| |world|hello, world| |C++|hello, C++| |Taiwan|hello, Taiwan| --- 不難發現, 輸出的格式都是 hello, {輸入} --- ##### 注意:逗點後有一個空格 ---- ### 參考答案 ```cpp= #include<iostream> using namespace std; int main(){ string text; cin>>text; cout<<"hello, "<<text; return 0; } ``` ---- #### 小試身手 [Zerojudge a002. 簡易加法](https://zerojudge.tw/ShowProblem?problemid=a002) ---- ### 參考答案 ```cpp= #include <iostream> using namespace std; int main(){ int n1, n2; cin>>n1>>n2; cout<<n1+n2<<endl; return 0; } ```

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully