search
尋找貓咪~QQ 地點 桃園市桃園區 Taoyuan , Taoyuan

C++ STL 容器基本範例實驗測試[05_CPP_STL_Set ->純C++ Set範例] – jashliao部落格

C++ STL 容器基本範例實驗測試[05_CPP_STL_Set ->純C++ Set範例]


資料來源:https://www.cnblogs.com/skyfsm/p/6934246.html

http://larry850806.github.io/2016/06/06/STL1/
http://larry850806.github.io/2016/06/06/STL2/

GITHUB 完整資料: https://github.com/jash-git/Base_CPP_STL_Example [ 包含免安裝開發環境:CodeBlocks 12.11 方便自己測試使用 ]

線上執行:https://www.tutorialspoint.com/compile_cpp_online.php / http://codepad.org/


Set 就是集合

只能拿走最上面的,或是繼續往上疊[先進後出]
基本功能有:
    ▲ insert: 把一個數字放進集合
    ▲ erase: 把某個數字從集合中移除
    ▲ count: 檢查某個數是否有在集合中
    ▲ size: 取個數
Set 的優點
    ▲ 操作很簡單
    ▲ 可以快速檢查裡面有沒有某個元素
Set 的缺點
    ▲ 當 Set 裡面東西太多時會拖慢速度
本範例額外紀錄

    ▲

#include 
#include 
#include /* malloc, free, rand */
#include /*strncpy .....*/
#include /*c++ string class*/
#include 
#include 
#include 
#include 

using namespace std;
/*
Set 就是集合
只能拿走最上面的,或是繼續往上疊[先進後出]
基本功能有:
    ▲ insert: 把一個數字放進集合
    ▲ erase: 把某個數字從集合中移除
    ▲ count: 檢查某個數是否有在集合中
    ▲ size: 取個數
Set 的優點
    ▲ 操作很簡單
    ▲ 可以快速檢查裡面有沒有某個元素
Set 的缺點
    ▲ 當 Set 裡面東西太多時會拖慢速度
本範例額外紀錄
    ▲
*/
void Pause()
{
    printf("Press Enter key to continue...");
    fgetc(stdin);
}

int main()
{
    set mySet;
    mySet.insert(20);   // mySet = {20}
    mySet.insert(10);   // mySet = {10, 20}
    mySet.insert(30);   // mySet = {10, 20, 30}
    cout<< mySet.size() << endl;
    cout<< "--------------------" << endl;

    cout << mySet.count(20) << endl;    // 存在 -> 1
    cout << mySet.count(100) << endl;   // 不存在 -> 0
    cout<< "--------------------" << endl;

    mySet.erase(20);                    // mySet = {10, 30}
    cout << mySet.count(20) << endl;    // 0
    cout<< mySet.size() << endl;
    cout<< "--------------------" << endl;
    mySet.clear();
    cout<< mySet.size() << endl;

    Pause();
    return 0;
}



熱門推薦

本文由 jashliaoeuwordpress 提供 原文連結

寵物協尋 相信 終究能找到回家的路
寫了7763篇文章,獲得2次喜歡
留言回覆
回覆
精彩推薦