遍历一个vector容器有非常多种方法。使用起来也是仁者见仁。

通过索引遍历:

for (i = 0; i<v.size(); i++)
{
cout << v[i] << " ";
}

迭代器遍历:

for (vInt::const_iterator iter = v.begin(); iter != v.end();iter++)
{
cout << *iter << " ";
}

算法遍历:

copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));

非常多书上推荐的是使用算法进行遍历。

写了一个简单的程序对上面的三种方法进行了比較:

#include<iostream>
#include<vector>
#include<iterator>
#include<algorithm>
#include<time.h>
#include<windows.h>
using namespace std;
typedef vector<int> vInt;
void print_vec_operator(const vInt & v)//方法一,採用下标訪问
{
int i;
for (i = 0; i<v.size(); i++)
{
cout << v[i] << " ";
}
cout << endl;
}
void print_vec_iterator(const vInt &v)//方法二,採用迭代器訪问
{ for (vInt::const_iterator iter = v.begin(); iter != v.end();iter++)
{
cout << *iter << " ";
}
cout << endl;
}
void print_vec_algorithm(const vInt &v)//方法三。将容器的内容拷贝到cout绑定的迭代器
{
copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
cout << endl;
} int main()
{
vInt v;
int i;
for (i = 0; i<100000; i++)
{
v.push_back(i);
}
int start_time_print_vec1 = GetTickCount();
print_vec_operator(v);
int end_time_print_vec1 = GetTickCount(); int start_time_print_vec2 = GetTickCount();
print_vec_iterator(v);
int end_time_print_vec2 = GetTickCount(); int start_time_print_vec3 = GetTickCount();
print_vec_algorithm(v);
int end_time_print_vec3 = GetTickCount(); std::cout << (end_time_print_vec1 - start_time_print_vec1) << endl;
std::cout << (end_time_print_vec2 - start_time_print_vec2) << endl;
std::cout << (end_time_print_vec3 - start_time_print_vec3) << endl; return 0;
}

当vector初始化10000个元素时,三种方法的效率不相上下。执行几次时间相差无几:

//输出:

//1718 operator[]

//1735 iterator

//1797 algorithm

可是当把veector初始化100000的时候,三种方法的效率就有了较大的差距:

//输出:

//20016 operator[]

//32172 iterator

//62468 algorithm

再写一个vector里放一个类:

#include<iostream>
#include<vector>
#include<iterator>
#include <algorithm>
#include <functional>
#include<windows.h> class AAA
{
public:
void MakeFull2()
{
}
}; int main()
{
int nCount = 1000000;
std::vector< AAA* > vAAA;
vAAA.resize(nCount);
for (int i = 0; i < nCount; ++i)
{
vAAA[i] = new AAA;
} // 时间
int start, end; // 測试成员函数调用(std::vector下标訪问方式)
start = GetTickCount();
size_t count = vAAA.size();
for (size_t i = 0; i < count; ++i)
vAAA[i]->MakeFull2();
end = GetTickCount();
std::cout << end - start << std::endl; // 測试成员函数调用(STL算法方式)
start = GetTickCount();
std::for_each(vAAA.begin(), vAAA.end(),
std::mem_fun<void, AAA>(&AAA::MakeFull2));
end = GetTickCount();
std::cout << end - start << std::endl; // 測试成员函数调用(STL迭代器方式)
start = GetTickCount();
std::vector< AAA* >::iterator itr_end = vAAA.end();
for (std::vector< AAA* >::iterator itr = vAAA.begin(); itr != itr_end; ++itr)
(*itr)->MakeFull2();
end = GetTickCount();
std::cout << end - start << std::endl; // 測试成员函数调用(STL迭代器方式)
start = GetTickCount();
for (std::vector< AAA* >::iterator itr = vAAA.begin(); itr != vAAA.end(); ++itr)
(*itr)->MakeFull2();
end = GetTickCount();
std::cout << end - start << std::endl;
return 0;
}
//输出:
//313 oprator[]
//62 algorithm
//422 iterator
//922 iterator

再执行一次,结果为:

//296

//63

//594

//1672

这个时候使用algorithm+functional进行遍历效率最高。

个人认为下标索引的方式总是会效率高于迭代器方式。

以下分析一下两种迭代器方式。为何相差不小呢:

这就要看一下std::vector::end()的原型了:

iterator end() _NOEXCEPT
{ // return iterator for end of mutable sequence
return (iterator(this->_Mylast(), &this->_Get_data()));
}

就是每次推断itr != vAAA.end()的时候,都要进行又一次构造一个迭代器并进行返回。这样当然减少的效率。

实战c++中的vector系列--vector的遍历(stl算法、vector迭代器(不要在循环中推断不等于end())、operator[])的更多相关文章

  1. spice在桌面虚拟化中的应用系列之三(USB映射实现,SSL加密,密码认证,多客户端支持)

    本系列其它文章 spice在桌面虚拟化中的应用系列之一(spice简介,性能优化等) spice在桌面虚拟化中的应用系列之二(Linux平台spice客户端的编译安装,支持USB映射) 1.spice ...

  2. 循环中不要放入openSession()

    for(Shop s:list) { System.out.println(s.getName()); String sql="select shopId,sum(ele_bank+ele_ ...

  3. 深入浅出:了解for循环中保留i值得方法

    一.保留i值  通常情况下,因为一些效果我们需要获取到for循环中的i的值,但是往往拿到的都是最后一个i的值.下面介绍几种方法可以获取到i的值 1.自定义属性: arr[i].index = i; 以 ...

  4. 实战c++中的string系列--std:vector 和std:string相互转换(vector to stringstream)

    string.vector 互转 string 转 vector vector  vcBuf;string        stBuf("Hello DaMao!!!");----- ...

  5. 实战c++中的vector系列--vector应用之STL的find、find_if、find_end、find_first_of、find_if_not(C++11)

    使用vector容器,即避免不了进行查找,所以今天就罗列一些stl的find算法应用于vector中. find() Returns an iterator to the first element ...

  6. 实战c++中的vector系列--构造、operator=和assign差别

    vector或许是实际过程中使用最多的stl容器.看似简单,事实上有非常多技巧和陷阱. 着重看一看vector的构造,临时依照C++11: default (1) explicit vector (c ...

  7. 实战c++中的vector系列--正确释放vector的内存(clear(), swap(), shrink_to_fit())

    关于vector已经写的差不多了,似乎要接近尾声了,从初始化到如何添加元素再到copy元素都有所涉及,是时候谈一谈内存的释放了. 是的,对于数据量很小的vector,完全没必要自己进行主动的释放,因为 ...

  8. 实战c++中的vector系列--copy set to vector(别混淆了reserve和resize)

    stl算法中有个copy函数.我们能够轻松的写出这种代码: #include <iostream> #include <algorithm> #include <vect ...

  9. 【实战Java高并发程序设计6】挑战无锁算法:无锁的Vector实现

    [实战Java高并发程序设计 1]Java中的指针:Unsafe类 [实战Java高并发程序设计 2]无锁的对象引用:AtomicReference [实战Java高并发程序设计 3]带有时间戳的对象 ...

随机推荐

  1. 数学课(math)

    数学课(math) 题目描述 wzy又来上数学课了-- 虽然他很菜,但是数学还是懂一丢丢的.老师出了一道题,给定一个包含nn个元素的集合P=1,2,3,-,nP=1,2,3,-,n,求有多少个集合A⊆ ...

  2. 【CCF】URL映射 模拟

    #include<iostream> #include<cstdio> #include<cstring> #include<string> #incl ...

  3. ios 瀑布流的那些事情

    转载: 屎壳郎情调-成长日记 首先要知道:瀑布流的核心就是要获取到图片的长宽 网上的很多例子都是加载本地图片的 对于新手而言 改成加载网络图片的确是有点压力的  因为本地的图片 我们是很容易就能获取到 ...

  4. webpack+babel+ES6+react环境搭建

    webpack+babel+ES6+react环境搭建 步骤: 1 创建项目结构 注意: 先创建一个项目目录  react  这个名字自定义,然后进入到这个目录下面 mkdir app //创建app ...

  5. Mybatis Plugin插件安装破解及使用

    2018年2月更新 2018年2月份,提供一个网上比较多的一个版本V3.21版本,下载资源里面有个已整合版直接解压放入C:\Users\你的用户名\.IntelliJIdea2017.3\config ...

  6. 【HDOJ1828&&POJ1177】Picture(线段树,扫描线)

    题意:给定n个矩形,求他们的并的周长 n<=5e3,abs(x[i])<=1e4 思路:From https://www.cnblogs.com/kuangbin/archive/2013 ...

  7. shell中规则表达式与特殊符号

    在 bash 的操作环境中还有一个非常有用的功能,那就是通配符 (wildcard) ! 我们利用 bash 处理数据就更方便了!底下我们列出一些常用的通配符喔: 符号 意义 * 代表『 0 个到无穷 ...

  8. 老郭带你学数据结构(C语言系列)2-线性表之动态顺序表

    一.基本概念: 线性表:由n个类型相同的数据元素组成的有限序列,记为(a1,a2,--an). 线性表的特征:其中的元素存在这序偶关系,元素之间存在着严格的次序关系. 顺序存储表:线性表中的元素依次存 ...

  9. python日期时间相关

    参考: http://www.coder4.com/archives/2239 http://www.cnblogs.com/lhj588/archive/2012/04/23/2466653.htm ...

  10. HDU5036 Explosion(期望&&bitset)

    #include <iostream> #include <cstring> #include <string> #include <vector> # ...