Vectors模型是一个动态数组。
需要添加头文件<vector>。
原型定义如下:
namespace std {
template <class T,
class Allocator = allocator<T> >
class vector;
}
allocator是指内存模型。
和其他容器类一样,vector将元素拷贝到其内部,而非想象中的引用。这样,如果你修改了原先的元素,vector中的元素并不会影响。
vector提供随机访问,其通过对[]进行重载来实现。
理解capacity对于vector非常重要。capacity指容器对象所能容纳的元素个数。可以通过reserve()来扩充capacity。这样,在后来添加元素的时候就无需再申请内存。
也可以一开始事先调用以下语句:
std::vector<T> v(5); // creates a vector and initializes it with five values
// (calls five times the default constructor of type T)
另外capacity只能扩充,不能缩小。如需缩小,需要通过另外的算法机制来实现。如下面所示。
template <class T>
void shrinkCapacity(std::vector<T>& v)
{
std::vector<T> tmp(v); // copy elements into a new vector
v.swap(tmp); // swap internal vector data
}
vector一些常用的操作
vector<Elem> c Creates an empty vector without any elements
vector<Elem> c1(c2) Creates a copy of another vector of the same type (all elements are copied)
vector<Elem> c(n) Creates a vector with n elements that are created by the default constructor
vector<Elem> c(n,elem) Creates a vector initialized with n copies of element elem
vector<Elem> c(beg,end) Creates a vector initialized with the elements of the range [beg,end)
c.~vector<Elem>() Destroys all elements and frees the memory
c1 = c2 Assigns all elements of c2 to c1
c.assign(n,elem) Assigns n copies of element elem
c.assign(beg,end) Assigns the elements of the range [beg,end)
c1.swap(c2) Swaps the data of c1 and c2
swap(c1,c2) Same (as global function)
*需要对assiign说明一点。在对vector对象重新赋值的同时已经将旧的原有元素删除。
c.at(idx) Returns the element with index idx (throws range error exception if idx is out of range)
c[idx] Returns the element with index idx (no range checking)
c.front() Returns the first element (no check whether a first element exists)
c.back() Returns the last element (no check whether a last element exists)
*以上四个操作,只有at()对idx进行安全检查,其余均不作任何检查。
c.begin() Returns a random access iterator for the first element
c.end() Returns a random access iterator for the position after the last element
c.rbegin() Returns a reverse iterator for the first element of a reverse iteration
c.rend() Returns a reverse iterator for the position after the last element of a reverse iteration
c.insert(pos,elem) Inserts at iterator position pos a copy of elem and returns the position of the new element
c.insert(pos,n,elem) Inserts at iterator position pos n copies of elem (returns nothing)
c.insert(pos,beg,end) Inserts at iterator position pos a copy of all elements of the range [beg,end) (returns nothing)
c.push_back(elem) Appends a copy of elem at the end
c.pop_back() Removes the last element (does not return it)
c.erase(pos) Removes the element at iterator position pos and returns the position of the next element
c.erase(beg,end) Removes all elements of the range [beg,end) and returns the position of the next element
c.resize(num) Changes the number of elements to num (if size() grows, new elements are created by their default constructor)
c.resize(num,elem) Changes the number of elements to num (if size() grows, new elements are copies of elem)
c.clear() Removes all elements (makes the container empty)
对于删除元素,vector并不提供对特定值的元素删除的操作。可以通过下面操作来实现:
std::vector<Elem> coll;
...
//remove all elements with value val
coll.erase(remove(coll.begin(),coll.end(),val),coll.end());
___________________________________________________
std::vector<Elem> coll;
...
//remove first element with value val
std::vector<Elem>::iterator pos;
pos = find(coll.begin(),coll.end(),
val);
if (pos != coll.end()) {
coll.erase(pos);
}
关于对异常的处理,vector提供非常小的逻辑错误检查。前面已经说过,只有at()可能抛出异常。
另外,STL承诺以下几点:
1.如果通过push_back()在插入元素的时候发生异常,那么push_back()将不起作用。
2.insert()要么插入成功,要么不起作用,前提是元素的拷贝构造函数和赋值函数不会抛出异常。
3.pop_back(),swap()不抛出任何异常。
4.如果元素的拷贝构造函数和赋值函数不会抛出异常,则erase()和clear()不抛出异常。