Apium
    • 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
    # Struct / Class 自訂一種資料型態 裡面能夠包含各種東西 方便程式的修改與除錯,亦美觀 和 pair 有點像,只是 pair 只包含兩個變數 如果要處理三個以上的值就不建議用 pair :star: [**學長講義**](/XToC9yD-R6Oxo7-G8nMy6Q) :star: 可以先:arrow_up:看這個 在 C++11 後 class/struct 的差別基本沒有 除了用在 template 的差異 另外 class 底下是預設 private struct 預設 public ,可以參考學長的講義 因為如果寫一般解題 code 並沒有要抓其他資料或隱私問題 用 struct 有時會比較方便,以下範例用 struct ,就不討論 private 的問題了 --- ### 宣告 ```cpp= struct 結構名稱 { //名稱可以自己取 ... }; //記得加分號 ``` 這邊是宣告一種泛用結構 並不是宣告一個變數 所以結構名稱並不能直接用 要再在 main 裡宣告屬於這個結構的變數 ex. ```cpp= struct position { int x; int y; }; int main() { position dot; //宣告型態為 position,名稱為 dot 的變數 } ``` ### 初始化 初始化的方式百百種 最簡單的就是手動初始化 ```cpp= int main() { position dot; dot.x = 0; dot.y = 0; } ``` 不過有幾點要注意 結構內的變數並不會自動初始化 像上方範例的 `dot` 裡面的 `x, y` 就是殘值 而跟一般變數一樣,如果是被宣告成全域變數,會自動初始化為 0 不過還有一種方法 這樣寫會讓所以新宣告且未初始化的新變數設為預設值 ```cpp= struct position { int x = 0; int y = 0; }; int main() { position dot; //0 0 } ``` ### 取用 在宣告的變數後加上想要取用的成員變數名稱 ```cpp= struct position { int x; int y; }; int main() { position dot; dot.x = 4; dot.y = 6; cout << dot.x; //4 cout << dot.y; //6 } ``` ### 搭配 雖然學長的講義用了陣列 但推薦大家用 STL 容器來搭配 ex. vector ```cpp= vector<position> vec(10); vec[0].x = 10; ``` ### 成員函式 成員函式是專門為該 struct 所適配的函式 方便適讀,也方便 debug 當在呼叫的時候,會自動傳入呼叫的 struct 變數 所以可以不需要導入變數就可以使用資料成員 當然也可以代入參數 ```cpp= struct position { int x; int y; void swap() { int tmp = x; x = y; y = tmp; } void print(string str) { cout << x << ' ' << y << ' ' << str << '\n'; } }; int main (){ position dot = {10, 5}; dot.swap(); //函式當中的x, y在呼叫時等同於dot.x, dot.y dot.print("nothing"); //5 10 nothing } ``` ### 建構元 學長講義有提到,但比較少用 大部分都用 `initializer_list` 比較方便 上篇 map 的初始化也有偷偷用到 但要注意 建構元沒有回傳值 ( 不是 void !! ) 而且建構元的函式名稱要跟結構名稱一樣 ```cpp= struct position { int x; int y; position(int a, int b){ x = a, y = b; } }; ``` --- <span style="display: flex; justify-content: center; align-content: center; color: black; background-color: rgba(120, 200, 200);"> **以下是補充** </span> --- ## this指標 `this` 是只能在非靜態成員函式中 它會指向針對其呼叫成員函式的物件 像上面提到我們在使用成員函式時 函式是利用一個已經被宣告的變數來呼叫函式 i.e. `position dot; dot.swap();` 就是用 dot 來呼叫 swap 所以如果我們在 swap 中使用 this指標 時 他在當下就是代表 dot 的指標,也就是隱含 dot 的記憶體位置 而用法跟一般指標相同 ```cpp= struct position { int a; int b; void swap() { int tmp = this->a; (*this).a = b; b = tmp; } }; int main (){ position dot = {10, 5}; dot.swap(); cout << dot.a << ' ' << dot.b; } ``` 若 struct 中有對當前 struct 型態的指標,也可以防止自我導向 ```cpp= struct position { int value; position *ptr = nullptr; void test() { if(&ptr != this) cout << "not self-reference"; } }; ``` ###### tags: `初階班`

    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