# C++ STL ## set, tuple 陳宏彰 [slides](https://hackmd.io/@simba-fs/cpp-stl) --- # Set ```cpp= #include <set> using namespace std; int main(int argc, char *argv[]){ // some code here } ``` ---- # Introduction ![img of set](https://i.imgur.com/HIeaLY4.png) ---- # Method 1. insert(n) 把一個數字 n 放進集合 2. erase(n) 把某個數字 n 從集合中移除 3. count(n) 檢查某個數 n 是否有在集合中 ---- # Advantages and disadvantages ---- ## Advantages 操作很簡單 可以快速檢查裡面有沒有某個元素 ---- ## Disadvantages 當 Set 裡面東西太多時會拖慢速度 ---- # Example ```cpp= #include <iostream> #include <set> using namespace std; int main(int argc, char *argv[]){ set<int, greater<int> > mySet; mySet.insert(10); mySet.insert(20); mySet.insert(30); mySet.insert(40); mySet.insert(50); cout << "10 " << mySet.count(10) << '\n'; // 1 cout << "20 " << mySet.count(20) << '\n'; // 1 cout << "30 " << mySet.count(30) << '\n'; // 1 cout << "40 " << mySet.count(60) << '\n'; // 0 mySet.erase(20); cout << "20 " << mySet.count(20) << '\n'; // 0 return 0; } ``` ---- # See more [set](https://en.cppreference.com/w/cpp/container/set) ---- # Have a try [b050](https://zerojudge.tw/ShowProblem?problemid=b050) --- # Tuple ```cpp= #include <tuple> using namespace std; int main(int argc, char *argv[]){ // some code here } ``` ---- # Introduction Tuple let you combine two or more value into one vaiable ---- # Method ```cpp= make_tuple using namespace std; tuple<double, char, string> get_student(int id) { if (id == 0) return make_tuple(3.8, 'A', "Lisa Simpson"); if (id == 1) return make_tuple(2.9, 'C', "Milhouse Van Houten"); if (id == 2) return make_tuple(1.7, 'D', "Ralph Wiggum"); throw invalid_argument("id"); } ``` ---- # Example ```cpp= #include <iostream> #include <tuple> using namespace std; int main(int argc, char *argv[]){ // 1 tuple<int, int, int> t1 = {1, 3, 4}; cout << get<0>(t1) << '\n'; // 1 cout << get<1>(t1) << '\n'; // 3 cout << get<2>(t1) << '\n'; // 4 return 0; } ``` ---- # See more [tuple](https://en.cppreference.com/w/cpp/utility/tuple)
{"metaMigratedAt":"2023-06-15T02:13:14.098Z","metaMigratedFrom":"YAML","title":"C++ STL","breaks":true,"contributors":"[{\"id\":\"e9395e03-a75e-451d-8392-22ff7e9c3ef5\",\"add\":3483,\"del\":1367}]"}
    325 views