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