Zi 字媒體
2017-07-25T20:27:27+00:00
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;
}
寫了
5860316篇文章,獲得
23313次喜歡