Vector:
内存的分配:为了支持快速随机访问
vector容器的元素以连续的方式存放,每一个元素紧挨着前一个元素存储。为了使vector容器能够实现快速的内存分配,其实际分配的容量要比当前所需的空间要多一些,vector容器预留了额外的存储区,用于存放新添加的元素。所以
capacity所返回的值大于size.而且
clear()/erase()可以减小vector的size,但并不能减少capacityvector
对象的重要属性就在于可以在运行时高效的添加元素vector
常用的操作:empty
():判断是否为空格size
():返回v中元素的个数push_back(t):
在末尾增加一个值为t的元素[n]:
返回v中位置为n的元素,注意下标操作不能添加元素= ==
!= <= >= < >begin,end
函数用于返回迭代器:begin返回第一个元素,end返回最后一个元素的下一个元素c.insert(p,t),p
所指位置插入t元素,返回新加元素的迭代器c.insert(p,n,t)p
所指位置插入n个值为t的元素,返回void类型c.insert(p,b,e),p
所指位置插入b到e范围内的元素,返回void类型
list /deque
push_back(t):
在末尾增加一个值为t的元素push_front(t):
在前端增加一个值为t的元素c.insert(p,t),p
所指位置插入t元素,返回新加元素的迭代器c.insert(p,n,t)p
所指位置插入n个值为t的元素,返回void类型c.insert(p,b,e),p
所指位置插入b到e范围内的元素,返回void类型
共同的操作:
c.back
():返回最后一个元素的引用c.front
():返回第一个元素的引用c[n]:
返回下标为n的元素的引用 for vector and dequeuec.at(n):
返回下标为n的元素的引用 for vector and dequeuec.erase(p)
删除迭代器所指元素c.clear()
删除所有的元素c.pop_back()
删除容器最后一个元素c.pop_front()
删除容器第一个元素c.assign()
重新赋值c.swap()
交换内容Iterator
Vector,deque支持迭代器算术运算,比较运算
List不支持算术运算,比较运算,只支持常用的++/--/==
标准库为每一种标准容器定义了一种迭代器类型,例如
vector<int>::iterator iter;*
:解引用操作符来访问迭代器所指向的元素++
、--:自增自减操作符==
!=【】可以通过下标操作符,改变元素的值,但不能添加元素
算术运算
const_iterator:只读迭代器只用于读取容器内元素的值,但不能改变其值
reverse_iterator:按逆序寻址元素的迭代器,反转了某些相关的迭代器操作,例如++将指向容器中的前一个元素。
c.begin:
返回一个迭代器,指向容器第一个元素c.end:
返回一个迭代器,指向容器最后一个元素c.rbegin:
返回一个迭代器,指向容器最后一个元素c.rend: 返回一个迭代器,指向容器第一个元素
练习如下:
# include <iostream>
using namespace std;
# include <vector>
# include <iterator>
# include <list>
# include <deque>
int main()
{
vector<int> ivec(10); //container:vector and iterato
for(vector<int>::iterator ix= ivec.begin();ix != ivec.end();++ix) //iterator
{
*ix = 3;
cout<<"The value "<< "is "<< *ix<<endl;
}
vector<int>::iterator mid1 = ivec.begin()+ivec.size()/2;
// vector<int>::iterator mid2 = (ivec.begin() + ivec.end())/2;
cout <<"The value of mid1 is"<<mid1<<endl;
// cout <<"The value of mid2 is"<<mid2<<endl;
//const_iterator
for(vector<int>::const_iterator it = ivec.begin(); it != ivec.end(); ++it)
{
cout<<"The value "<< "is "<< *it<<endl;
}
//reverse_iterator
for(vector<int>::reverse_iterator itq = ivec.rbegin();itq != ivec.rend();++itq)
{
*itq = 6;
cout<<"The value "<< "is "<< *itq<<endl;
}
ivec.push_back(5);
vector<int>::iterator iit = ivec.begin();
ivec.insert(iit,8);
cout<<"Check whether insert work"<<endl;
for(vector<int>::const_iterator itz = ivec.begin(); itz != ivec.end(); ++itz)
{
cout<<"The value "<< "is "<< *itz <<endl;
}
cout<<"The back value is "<<ivec.back()<<endl;
cout<<"The front value is "<<ivec.front()<<endl;
vector<int>::iterator itr = ivec.begin()+3;
ivec.erase(itr);
for(vector<int>::const_iterator its = ivec.begin(); its != ivec.end(); ++its)
{
cout<<"The value "<< "is "<< *its <<endl;
}
ivec.clear();
cout<<"Clear is called"<<endl;
ivec.assign(20,33);
vector<int> ivec2;
ivec2.assign(20,55);
ivec.swap(ivec2);
vector<int> ivec3;
for (vector<int>::size_type i = 0;i != 10;i++)
ivec3.push_back(i);
for(vector<int>::const_iterator itss = ivec.begin(); itss != ivec.end(); ++itss)
{
cout<<"The value "<< "is "<< *itss <<endl;
}
cout<<"The size of ivec is"<<ivec3.size()<<endl;
cout<<"The capacity of ivec is" <<ivec3.capacity()<<endl;
ivec3.erase(ivec3.begin()+3);
cout<<"The size of ivec is"<<ivec3.size()<<endl;
cout<<"The capacity of ivec is" <<ivec3.capacity()<<endl;
return 0;
}
运行结果如下:
The value is 3
The value is 3
The value is 3
The value is 3
The value is 3
The value is 3
The value is 3
The value is 3
The value is 3
The value is 3
The value of mid1 is00491DE4
The value is 3
The value is 3
The value is 3
The value is 3
The value is 3
The value is 3
The value is 3
The value is 3
The value is 3
The value is 3
The value is 6
The value is 6
The value is 6
The value is 6
The value is 6
The value is 6
The value is 6
The value is 6
The value is 6
The value is 6
Check whether insert work
The value is 8
The value is 6
The value is 6
The value is 6
The value is 6
The value is 6
The value is 6
The value is 6
The value is 6
The value is 6
The value is 6
The value is 5
The back value is 5
The front value is 8
The value is 8
The value is 6
The value is 6
The value is 6
The value is 6
The value is 6
The value is 6
The value is 6
The value is 6
The value is 6
The value is 5
Clear is called
The value is 55
The value is 55
The value is 55
The value is 55
The value is 55
The value is 55
The value is 55
The value is 55
The value is 55
The value is 55
The value is 55
The value is 55
The value is 55
The value is 55
The value is 55
The value is 55
The value is 55
The value is 55
The value is 55
The value is 55
The size of ivec is10
The capacity of ivec is16
The size of ivec is9
The capacity of ivec is16