C++ 入門指南 4.01
練習 16.1 參考程式 - 練習標準程式庫的 vector
// 引入標準程式庫中相關的輸入、輸出程式
#include <iostream>
// 引入標準程式庫中的 vector
#include <vector>
// cout 為 std 中的輸出物件
using std::cout;
// endl 為 std 中的斷行符號
using std::endl;
// vector 為 std 中的集合體型態
using std::vector;
int main(void) {
// 宣告建立整數 vector
vector<int> v1;
vector<int> v2(5);
vector<int> v3(5, 1);
vector<int> v4 = {1, 2, 3, 4, 5};
vector<int> v5 {1, 2, 3, 4, 5};
vector<int> v6({1, 2, 3, 4, 5});
// 印出 v2 中的第一個元素
cout << v2[0] << endl;
// 印出 v3 中的第一個元素
cout << v3[0] << endl;
// 印出 v4 中的最後一個元素
cout << v4[4] << endl;
// 印出 v5 中的第三個元素
cout << v4[2] << endl;
// 印出 v6 中的第四個元素
cout << v4[3] << endl;
// 最後回傳 0 給作業系統
return 0;
}
/* 《程式語言教學誌》的範例程式
http://kaiching.org/
檔名:exercise1601.cxx
編譯:g++ exercise1601.cxx -std=c++11
執行:./a.out
功能:C++入門指南單元十六的練習
作者:張凱慶 */
回到練習題目