Vector ======== Introduction -------- Vector是c++中陣列的替代型態,可以自主控制需要的記憶體。 Vector可以任意增加陣列長度及資料的數量,也可任意插入或刪除指定位置的資料。 基礎運用 -------- 使用vector需要加入標頭檔 ex: ```c #include ``` 建立新vector的語法 vector vector_name(amount,element); ex: ```c vector acm(3,4); vector csie(5); ``` 嘗試印出兩vector內容 output: ```c acm : 4 4 4 csie : 0 0 0 0 0 ``` 也可以直接建立空白的vector ex: ```c vector acm; ``` 若要讀取資料的話需要先定義iterator(迭代器)以進行進一步的操作 ```c vactor ::iterator it; vactor ::iterator it2; ``` 完整程式碼範例 ex: ```c #include #include using namespace std; vector acm(3,4); vector::iterator it; void vector_print(){ cout << "The vector contains these elements : " << endl; for(it = acm.begin(); it != acm.end(); it++){ cout << *it << " "; }//prints the vector from the first element to the last element cout << endl; } int main(){ int n; vector_print(); return 0; } ``` Output: ```c The vector contains these elements : 4 4 4 ``` Member functions ---------------------------- ### operator[] > ### .push_back() ### .pop_back() ### .begin() / .end() >.begin()會將iterator指向第一筆資料 >.end()會將iterator指向最後一筆資料 ### .size() >.size會回傳vector大小 ### .clear() / .empty() ### .erase() / .insert() ### .assign()