摘要: C++标准库为迭代器提供了三个辅助函数:advance(), distance(), iter_swap() ,前两者提供给所有迭代器一些原本只有 Random Access 迭代器才有的能力:前进(或后退)多个元素,及处理迭代器之间的距离。第三个辅助函数允许你交换两个迭代器的值。#include <iostream>#include <vector>#include <algorithm>int main(){ std::vector<int> vect = {1,2,3,4}; std::vector<int>::iterator 阅读全文
posted @ 2013-05-15 16:27 轻典 阅读(213) 评论(0) 推荐(0) 编辑
摘要: vector 的定义如下(deque\list 类似):namespace std { template <class T, class Allocator = allocator<T> > class vector;}第二个参数是用来定义内存模型,缺省使用C++标准库提供的 allocator。其 capacity() 方法返回 vector 实际能够容纳的元素数量,如果超越这个数量,vector 就有必要重新配置内部存储器,和 vector 元素相关的 references\pointers\iterators 都会失效,另外内存重新分配也很耗时间。所以可以使用 r 阅读全文
posted @ 2013-05-15 14:31 轻典 阅读(184) 评论(0) 推荐(0) 编辑