Chieh-Chang Hung
    • 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 New
    • 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 Note Insights Versions and GitHub Sync 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
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    --- title: 現代 CPP. 容器. tags: cpp description: View the slide with "Slide Mode". --- # 現代 CPP. 容器 ###### tags: `CPP` - [slide:https://hackmd.io/@ewnlm/H1swdFAca] <!-- Put the link to this slide here so people can follow --> --- # Lambda expression - C++11 新功能。類似函式,但不需用名稱。 - Statment - y = a+b; - Expression - a+b - 在C/C++裡,函式必須先宣告才能使用,而且不能在函式裡再宣告新函式。 : - **Lambda experssion**可以接受外部參數並做運算 :+1: ---- ```cpp= int op_code(int a, int b) { int _mul2(int din) { return din<<2; } // Compile error. auto _mul2 = [](int din)->int { return din<<2; } // OK. Lambda expression. int const_nu (4); // for_each in <algorithm> vector<int> some_data {0, 2, 3, 4} ; for_each(some_data.begin(), some_data.end(), _mul2); // ref or value } ``` ---- # Syntax ```cpp= [capture](parameter)->trailing-type { body } ``` - capture. 擷取外部變數於body使用。 - parameter. 代入的參數,類似函式的參數列。 - trailing-type. body的回傳值。 - body. 主題 - 詳情請參閱[Lambda expression](https://en.cppreference.com/w/cpp/language/lambda) :thinking_face: --- # [容器](https://en.cppreference.com/w/cpp/container) - 顧名思義,就是存放資料的地方。 - 分兩大類,循序式及關聯式,與各自儲存的機制有關, - Sequence container - Associate container - 容器 + <algorithm> 幫助你快速實現你的想法 :sunglasses: --- # Example 1. - :question: 不同變數執行相同函式 - 設定每個訊號的極性。 ```cpp= polarity_set(signal1, true); polarity_set(signal2, true); polarity_set(signal3, true); polarity_set(signal4, false); polarity_set(signal5, false); ``` ---- ## Solution - 利用for\_each以及lambda. ```cpp= vector<SIGNAL> true_cond { signal1, signal2, signal3 }; for_each(true_cond.begin(), true_cond.end(), [](SIGNAL din) ->void { polarity_set(din, true); }); ``` ---- ### Spot light - list inistialize 直接列出vector的內容。 - iterator of vector. [vector begin](https://en.cppreference.com/w/cpp/container/vector/begin) - for_each()類似 range-based for loop, 形式不同而已。 - for_each()頭兩個參數是指定容器的範圍,第三個參數是動作,函式或lambda. - 若需要更改容器內容,記得用參考. --- # Example 2. - :question: 設計Shmoo圖外框。 - 能跟隨不同軸的長度變化。 - `#`標示10倍數, `+`標示5倍數, 其餘以`-`標示。 ---- ## Solution ```cpp= string boundary; vector<int> steps(N); iota(begin(steps), end(steps), 0); // let steps from 0~N transform(steps.begin(), steps.end(), back_inserter(boundary), [](int i)->char { return ((i%10)==0)?('#'):((i%5)==0)?('+'):('-');}) // base on steps and generate boundary of SHMOO. ``` ---- ### Spot light - 目的是產生SHMOO邊框,為了方便看,在5與10倍數對應位置上標示符號。 - 利用iota()產生數列。 - transform()可以利用其他容器的內容來決定新容器。 - 所以利用steps數列去產生新的string. --- # Example 3. - :question: 找出SHMOO邊界值。 - 可能有 Pass to Fail or Fail to Pass. - 也可能全滿或是全空的狀況。 ---- ## Solution ```cpp= vector<PFState> result; auto bound1 = adjacent_find(begin(result), end(result), [](auto a, auto b)->bool { return(a!=b)?(true):(false); }); auto bound2 = adjacent_find(next(bound1, 1), end(result), [](auto a, auto b)->bool { return(a!=b)?(true):(false); }); ``` ---- ### Spot light ```cpp= auto bound1 = adjacent_find(result.begin(), result.end()); ``` - adjacent_find() 本意是找出鄰近兩兩相同的位址,回傳迭代器。 - 但我們可以修改成尋找兩兩不相同的位址。 - 為了確認SHMOO是否有破洞,可以更改尋找的起始位址。 - next(iter, N) 可以回傳iter後第N個迭代器。 --- # Example 4. - :question: Testbench裡,如何用文字找到對應函式. ---- ## Solution ```cpp= #define COUPLE(sth) make_pair(#sth, sth) using fptrTest = void(*)(void); typedef void (*fptrTest)(void); void RDID_test(); unordered_map<string, fptrTest> store_um { {"RDID_test", RDID_test}, COUPLE(NR_test), COUPLE(BE_test), COUPLE(PP_test), } ``` ---- ## Solution ```cpp= string item ("NR_test"); if(store_um.find(item)!=store_um.end()) { store_um.at(item)(); } ``` ---- ### Spot light - 利用map建立字串與函式指標的關係。unordered_map搜尋較map快. - 利用巨集簡化. 詳閱macro stringication. - find()用來尋找Key是否存在. - 如果不存在, 通常會回傳 end() iterator. - at(Key)回傳對應Value.用字串去回傳對應函式, 因為要執行此函式, 背後再加圓括弧. :baby_bottle: - :space_invader: 當Key是struct or class 該怎麼處理? --- ### Thank you! :sheep:

    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