Severity: Notice
Message: Undefined offset: 1
Filename: infosekolah/leftmenudasboard.php
Line Number: 33
Line Number: 34
array
bitset
deque
forward_list
list
map
multimap
multiset
queue
set
stack
unordered_map
unordered_multimap
unordered_set
unordered_multiset
vector
fstream
iomanip
ios
iosfwd
iostream
istream
ostream
sstream
streambuf
charconv
codecvt
regex
string
string_view
algorithm
any
chrono
execution
filesystem
functional
initializer_list
iterator
locale
optional
system_error
|tuple
type_index
typeinfo
type_traits
utility
valarray
variant
atomic
condition_variable
future
mutex
shared_mutex
thread
complex
limits
numeric
random
ratio
exception
stdexcept
new
memory
memory_resource
scoped_allocator
cassert
ccomplex
cctype
cerrno
cfenv
cfloat
cinttypes
ciso646
climits
clocale
cmath
csetjmp
csignal
cstdalign
cstdarg
cstdbool
cstddef
cstdint
cstdio
cstdlib
cstring
ctime
ctgmath
cuchar
cwchar
cwctype
Vector 是C++標準程式庫中的一個類,可視為會自動擴展容量的陣列,以循序(Sequential)的方式維護變數集合。
vector的特色有支持隨機存取,在集合尾端增刪元素很快,但是在集合中間增刪元素比較費時。
vector是C++標準程式庫中的眾多容器之一。
vector以模板方式實現,可以保存任意類型的變數,包括使用者自定義的資料型態,例如:它可以是放置整數(int)型態的vector、也可以是放置字串(string)型態的vector、或者放置使用者自定類別(user-defined class)的vector。
vector 定義於 <vector> 標頭檔中。與其他STL元件一樣,vector 屬於std名稱空間。
vector是C++標準程式庫裡最基本的容器,大多數狀況下都很有效率。vector設計之初即是為了改善C語言原生陣列的種種缺失與不便,而欲提供一種更有效、更安全的陣列。vector的使用介面刻意模擬C語言原生陣列,較明顯的差異在於記憶體管理,原生陣列必須在宣告陣列的時候明確指定陣列長度(例如 int a[5]),但是 vector 不需要指定,而是會在執行期依據狀況自我調整長度,動態增大容量。
vector的表現一如資料結構中的陣列,允許隨機存取(Random Access),以索引值(index)存取任一元素只要花費常數時間 O(1),在集合尾端增加或刪除元素也是花費常數時間O(1),若在vector集合中間增加或刪除元素時間複雜度是線性時間O(n),較為費時。雖然C++標準並沒有規定實作方式,但大多數 vector 內部均使用動態陣列方式實作。
vector 類別是以容器 模式為基準設計的,也就是說,基本上它有 begin(),end(),size(),max_size(),empty() 以及 swap() 這幾個方法。
begin()
end()
size()
max_size()
empty()
swap()
vec[i]
vec.at(i)
vec.front()
vec.back()
vec.push_back()
vec.pop_back()
vec.insert()
vec.erase()
vec.clear()
vec.size()
vec.empty()
vec.capacity()
vec.reserve()
vec.resize()
vec.begin()
vec.end()
vec.rbegin()
vec.rend()
使用 vector 之前,必須先 #include<vector>。
声明一個 vector 變數的方法如下:
std::vector<T> v;
T 是 vector 要儲存的物件集合的型別,該 vector 的變數名稱是 v。T 可以是任何符合 Copy/Move Assignable 條件的型別,包括使用者自訂型別。如果 T 不符合 Copy / Move Assignable 或者複製 / 移动成本很高昂,可以考慮使用 T* 甚至 std::unique_ptr<T> 來代替 T。
#include <vector> #include <iostream> int main() { std::vector<int> v; v.push_back(1); v.push_back(2); v.push_back(3); for(int i=0;i<3;++i) std::cout << v[i] << std::endl; system("pause"); return 0; }
以下程式碼是用來說明 vector 的長度變化。
//Headers and Macros #include <iostream> #include <cstdlib> #include <vector> #include <iomanip> #define SETW_1 10 #define SETW_2 6 #define SETW_3 10 using namespace std; typedef vector<int> Vint; //利用參照取得真正的 capacity 值 void PrintVectorInfo(Vint& v) { cout<<setw(SETW_1)<<"Element"<<setw(SETW_2)<<"Size"; cout<<setw(SETW_3)<<"Capacity"<<endl; for ( Vint::iterator it = v.begin(); it != v.end(); it ++) { cout<<setw(SETW_1)<<(*it)<<setw(SETW_2)<<v.size(); cout<<setw(SETW_3)<<v.capacity()<<endl; } cout<<endl; } //Main Function int main(int argc, char** argv) { //==START==// //宣告一個 vector Vint vint; //宣告兩個整數變數 int a = 11, b = 22, c = 33; //建立只有一個元素空間的 vint //把變數 a 複製至第一個元素內 vint.push_back(a); cout<<"Push Back: a = "<<a<<endl; //建立兩個元素空間的 vint //把變數 a 複製至第一個元素內 //把變數 b 複製至第二個元素內 //刪除上一次建立的 vint //上一次建立的 vint 只有一個元素空間 //依此類推 vint.push_back(b); cout<<"Push Back: b = "<<b<<endl; vint.push_back(c); cout<<"Push Back: c = "<<c<<endl; PrintVectorInfo(vint); //移除最後一個元素 vint.pop_back(); cout<<"Pop Back......"<<endl; PrintVectorInfo(vint); //移除最後一個元素 vint.pop_back(); cout<<"Pop Back......"<<endl; PrintVectorInfo(vint); //清除所有元素 vint.clear(); cout<<"Clear All Elements."<<endl; //==END==// system("pause"); return 0; }
第一种:使用迭代器进行遍历
#include <iostream> #include <string> #include <vector> using namespace std; int main() { vector<string> v(3, "I Love Wikipedia "); // 元素个数,每个元素的值相同 for (vector<string>::const_iterator it = v.begin(); it < v.end(); ++it) // 输出Vector元素 cout << *it << endl; system("pause"); return 0; }
输出结果为:
I Love Wikipedia I Love Wikipedia I Love Wikipedia
第二种,与int类型相同
#include<string> #include <vector> #include <iostream> using namespace std; int main() { vector<string> vec; string str; str = "I Love WikiPedia"; vec.push_back(str); vec.push_back("123"); for(int i = 0; i < vec.size() ; ++i) { cout << vec[i] << endl; } system("pause"); return 0; }
I Love Wikipedia 123
vector 的成员函数 clear() 来删除所有的元素。这个操作并没有改变容器的容量,所以容量不變。
使用 vector 的成员函数 pop_back() 来删除容器尾部的元素。
成员函数 swap(),这个函数用来交换两个 vector 容器中的元素。因此可以与一个具有相同数据类型的内容为空的局部变量swap,从而实现彻底删除元素、释放容量的目的。
成员函数 shrink_to_fit(),会造成容器现有的迭代器都失效。
成员函数 erase() 有2个重载版本:
algorithm 头文件中的std::remove(),可以移除一个范围内匹配特定值的元素。remove() 是一个全局函数,所以它不能删除容器中的元素。会保持未移除的元素的顺序。真正删除需要用erase()成员函数,这叫做 erase-remove。remove() 算法返回的迭代器作为 erase() 的第一个参数,erase() 的第二个参数是所指向容器中最后一个元素后一个位置的迭代器。如:
words.erase(std::remove(std::begin(words), std::end(words),"none"), std::end(words));