size Return size (public member function )
max_size Return maximum size (public member function )
resize Change size (public member function )
capacity Return size of allocated storage capacity (public member function )
empty Test whether vector is empty (public member function )
reserve Request a change in capacity (public member function )
shrink_to_fit Shrink to fit (public member function )
其中capacity是未重新分配内存前的容量,max_size是最大容量。reserve函数可以预置vector的容量,当vector的使用量大于reserve的预置量的时候,会以预置量的2倍分配内存。resize改变vector容量,可大可小。
2.数据读取
operator[] Access element (public member function )
at Access element (public member function )
front Access first element (public member function )
back Access last element (public member function )
重载运算符[ ]使vector可以像数组一样访问,v[0]---v[n-1]
v.at(size_type i),i可以是int型的,
back最后一个元素值
3.vector修改元素的函数
assign Assign vector content (public member function )
push_back Add element at the end (public member function )
pop_back Delete last element (public member function )
insert Insert elements (public member function )
erase Erase elements (public member function )
swap Swap content (public member function )
clear Clear content (public member function )
emplace Construct and insert element (public member function )
emplace_back Construct and insert element at the end (public member function )
assign的二种用法v.assign(n,elem),将n个elem值赋给v;v.assign(begin,end),其中begin和end是迭代器,可以是向量的或是数组的。push_back() 在vector的末尾插入一个元素。
pop_back()在vector的末尾删除一个元素
insert函数三种用法insert(pos,val),insert(pos,n,val),insert(pos,begin,end) 返回值是插入的第一个元素iterator,只有第一种情况才有返回值。插入是在pos位置插入,原本位于pos的和pos以后的都要后移。
erase函数 可以删除一个或者一个范围
iterator erase (iterator position);
iterator erase (iterator first, iterator last);
clear函数
void clear();
清除vector中所有元素,然后vector的size大小变为0
swap 函数
void swap (vector& x);
作用就是交换两个vector中的元素
2.迭代器函数
begin Return iterator to beginning (public member function )
end Return iterator to end (public member function )
rbegin Return reverse iterator to reverse beginning (public member function )
rend Return reverse iterator to reverse end (public member function )
cbegin Return const_iterator to beginning (public member function )
cend Return const_iterator to end (public member function )
crbegin Return const_reverse_iterator to reverse beginning (public member function )
crend Return const_reverse_iterator to reverse end (public member function )
使用迭代器遍历vector
vector::iterator it,end;
end=v.end();
for(it=v.begin();it!=end;it++)
cout<<*it;//*it就是容器中的数据
使用pos遍历
for(int i=0;i<v.size();++i)
cout<<v.at(i);