default (1) explicit vector (const allocator_type& alloc = allocator_type());
fill (2) explicit vector (size_type n);
vector (size_type n, const value_type& val,
const allocator_type& alloc = allocator_type());
range (3) template vector (inputiterator first, inputiterator last,
const allocator_type& alloc = allocator_type());
copy (4) vector (const vector& x);
vector (const vector& x, const allocator_type& alloc);
move (5) vector (vector&& x);
vector (vector&& x, const allocator_type& alloc);
initializer list (6) vector (initializer_listil,
const allocator_type& alloc = allocator_type());
// constructing vectors
#include #include int main ()
; std::vectorfifth (myints, myints + sizeof(myints) / sizeof(int) );
std::cout << "the contents of fifth are:";
for (std::vector::iterator it = fifth.begin(); it != fifth.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
賦值運算
void assign (initializer_listil);
// vector assign
#include #include int main ()
; third.assign (myints,myints+3); // assigning from array.
std::cout << "size of first: " << int (first.size()) << '\n';
std::cout << "size of second: " << int (second.size()) << '\n';
std::cout << "size of third: " << int (third.size()) << '\n';
return 0;
}
vec.at(n) /vec[n]
取vec在位置n處的元素
at()
提供了越界檢查的功能,使用起來更安全,同時比起
operator
來代價也更大。
vec.front():取vec的第乙個元素
back與front相對應,vec.back() :取vec最後乙個元素
vec.begin()返回指向vec第乙個元素的迭代器,end與begin相對應,vec.end()返回指向vec最後乙個元素下乙個位置的迭代器。注意與
front/back
的區別:乙個是直接指向
vec內的元素,乙個是指向
vec的迭代器。若想通過
end訪問末尾元素,必須使
iter-1
。
cbegin/cend:與begin/end用法類似,返回const_iterator型別迭代器【注意const
迭代器和
const_iterator
的區別】,crbegin/crend與之對應,迭代器元素取反。
rbegin/rend:與begin/end用法類似,迭代器反向取元素 。
#include #include using namespace std;
int main ()
; //vectormyvector(myint, myint+sizeof(myint)/sizeof(int))
//vectormyvector2(myvector.begin(), myvector.end()-1);
//push_back:在vector末尾新增元素
vectormyvector;
for (int i=0;i<20;i++)
cout << "-----------------push_back---------------" << endl;
for (vector::iterator it = myvector.begin();it != myvector.end();++it)
//pop_back:在vector末尾刪除元素
for (int i=0;i<5;i++)
cout << "-----------------pop_back---------------" << endl;
for (vector::iterator it = myvector.begin();it != myvector.end();++it)
//insert:插入元素
vector::iterator it;
it = myvector.begin();
//插入單個元素,位置可選
myvector.insert(it,100);
cout << "-----------------插入單個元素---------------" << endl;
for (vector::iterator it = myvector.begin();it != myvector.end();++it)
//插入多個元素
myvector.insert(it,2,200);
cout << "-----------------插入多個元素---------------" << endl;
for (vector::iterator it = myvector.begin();it != myvector.end();++it)
//插入另乙個vector
it = myvector.begin();
vectoranothervect(2,500);
myvector.insert(it,anothervect.begin(),anothervect.end());
cout << "-----------------插入另乙個vetor---------------" << endl;
for (vector::iterator it = myvector.begin();it != myvector.end();++it)
//插入乙個陣列
it = myvector.begin();
int myint[5] = ;
myvector.insert(it, myint, myint+5);
cout << "-----------------插入乙個陣列---------------" << endl;
for (vector::iterator it = myvector.begin();it != myvector.end();++it)
//erase:刪除特定位置元素
myvector.erase(myvector.begin()+5);
cout << "-----------------刪除某個元素---------------" << endl;
for (vector::iterator it = myvector.begin();it != myvector.end();++it)
//erase:刪除某區間元素
myvector.erase(myvector.begin(),myvector.begin()+5);
cout << "-----------------刪除區間元素---------------" << endl;
for (vector::iterator it = myvector.begin();it != myvector.end();++it)
//clear:清空vector
myvector.clear();
cout << "-----------------清空vector---------------" << endl;
for (vector::iterator it = myvector.begin();it != myvector.end();++it)
return 0;
}
返回指向容器內部用於儲存自身元素的陣列的直接指標。
int main ()
{ std::vectormyvector (5);
int* p = myvector.data();
*p = 10;
++p;
*p = 20;
p[2] = 100;//指向myvector第三個元素
std::cout << "myvector contains:";
for (unsigned i=0; i
判斷vector是否為空
交換同型別的兩個vector
ref:
C STL感知 vector容器
vector容器非常類似陣列,也稱單端陣列 vector容器的迭代器支援隨機訪問與陣列不同 陣列是在靜態空間分配,vector可以動態擴充套件動態擴充套件原理 重新分配空間,將原空間資料拷貝到新空間,再釋放原空間建立vector容器void get vector void show vector v...
C STL中的容器 Vector
c stl中的verctor好比是c語言中的陣列,但是vector又具有陣列沒有的一些高階功能。與陣列相比,vector就是乙個可以不用再初始化就必須制定大小的邊長陣列,當然了,它還有許多高階功能。要想用vector首先得包含標頭檔案vector。如果vector的元素型別是int,預設初始化為0 ...
C STL容器的學習 vector
簡單點來說vector就是乙個模板類,而且是連續的 list不是連續的 刪除中間的任意乙個元素,後面的元素都需要往前移動,但對於陣列來說,它可以擴容.vectorv1 最大容量,初始值 vetcorv1.push back i 向v1的尾部插入i vectorv1.預設v1為空,故不能賦值 vect...