C++ 入門指南 4.01
練習 20.5 參考程式 - 練習標準程式庫的 set
// 引入標準程式庫中相關的輸入、輸出程式
#include <iostream>
// 引入標準程式庫中的 set
#include <set>
// cout 為 std 中的輸出物件
using std::cout;
// endl 為 std 中的斷行符號
using std::endl;
// set 為 std 中的集合體型態
using std::set;
int main(void) {
// 宣告建立整數 set
set<int> s {1, 2, 3, 4, 5};
// 刪除 set 中指定的元素
for(auto it = s.begin(); it != s.end();) {
if(*it == 2 || *it == 3)
it = s.erase(it);
else
++it;
}
// 利用迴圈印出 set 元素
for (int e: s) {
cout << e << endl;
}
// 最後回傳 0 給作業系統
return 0;
}
/* 《程式語言教學誌》的範例程式
http://kaiching.org/
檔名:exercise2005.cxx
編譯:g++ exercise2005.cxx -std=c++11
執行:./a.out
功能:C++入門指南單元二十的練習
作者:張凱慶 */
回到練習題目