is a collection of objects. is often referred to as a container because it “contains” other objects.
🤝template不是class,更像是一个操作让编译器做,从而去产生一个class,他需要额外的信息,而额外的信息用尖括号包围。
Define and Initialize
vector<int> ivec; // default to empty
vector<int> ivec2(ivec); // copy
vector<int> ivec3 = ivec; // copy
vector<int> ivec4(10, 1); // 10 elements with each 1
vector<int> ivec5(10); // 10 elements with each 0
vector<string> art = {“a”, “ab”}; // list init
vector<string> art2{“a”, “ab”}; // list init
vector<string> art3(“a”, “ab”); // error
vector<string> art5{10, “hi”}; // construct
这么多个例子只是为了对照,不为了记忆。
Using an Array to Initialize a vector
int arr[] = {0, 1, 2, 3}
vector<int> ivec(arr+1, arr+2);
construct with begin and end pointer arguments.(左开右闭)
Operators
operators | effect |
---|---|
[] | subscript |
= | assign, copy to; the r-h operand can be either object or curly brace |
==, != | if is equal in every detail |
<, ⇐, >, >= | Have their normal meanings using dictionary ordering. |
Functions
member funcs | effect |
---|---|
unknown push_back(value) | add a element at the end |
bool empty() | true if is empty |
vector<type>::size_type size() | number of elements |
iterator funcs | 🔗 Iterators |