实战c++中的vector系列--vector的遍历(stl算法、vector迭代器(不要在循环中推断不等于end())、operator[])
遍历一个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[])的更多相关文章
- spice在桌面虚拟化中的应用系列之三(USB映射实现,SSL加密,密码认证,多客户端支持)
本系列其它文章 spice在桌面虚拟化中的应用系列之一(spice简介,性能优化等) spice在桌面虚拟化中的应用系列之二(Linux平台spice客户端的编译安装,支持USB映射) 1.spice ...
- 循环中不要放入openSession()
for(Shop s:list) { System.out.println(s.getName()); String sql="select shopId,sum(ele_bank+ele_ ...
- 深入浅出:了解for循环中保留i值得方法
一.保留i值 通常情况下,因为一些效果我们需要获取到for循环中的i的值,但是往往拿到的都是最后一个i的值.下面介绍几种方法可以获取到i的值 1.自定义属性: arr[i].index = i; 以 ...
- 实战c++中的string系列--std:vector 和std:string相互转换(vector to stringstream)
string.vector 互转 string 转 vector vector vcBuf;string stBuf("Hello DaMao!!!");----- ...
- 实战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 ...
- 实战c++中的vector系列--构造、operator=和assign差别
vector或许是实际过程中使用最多的stl容器.看似简单,事实上有非常多技巧和陷阱. 着重看一看vector的构造,临时依照C++11: default (1) explicit vector (c ...
- 实战c++中的vector系列--正确释放vector的内存(clear(), swap(), shrink_to_fit())
关于vector已经写的差不多了,似乎要接近尾声了,从初始化到如何添加元素再到copy元素都有所涉及,是时候谈一谈内存的释放了. 是的,对于数据量很小的vector,完全没必要自己进行主动的释放,因为 ...
- 实战c++中的vector系列--copy set to vector(别混淆了reserve和resize)
stl算法中有个copy函数.我们能够轻松的写出这种代码: #include <iostream> #include <algorithm> #include <vect ...
- 【实战Java高并发程序设计6】挑战无锁算法:无锁的Vector实现
[实战Java高并发程序设计 1]Java中的指针:Unsafe类 [实战Java高并发程序设计 2]无锁的对象引用:AtomicReference [实战Java高并发程序设计 3]带有时间戳的对象 ...
随机推荐
- Unity属性——AddComponentMenu
字面理解:添加 组件选项菜单 分析:可能是添加一个脚本或者组件到一个物体上 验证: 新建一个脚本:AttributeTest 提示:添加一个组件菜单属性,允许你放一个脚本在Compoent菜单下,来代 ...
- 用jquery动态生成的元素绑定事件
$(document).on("click",".class a",function(){ })
- BZOJ 2818: Gcd(欧拉函数)
GCDDescription 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. Input 一个整数N Output 如题 Sample Input 4 ...
- POJ 3104:Drying(二分)
题目大意:你有一台机器可以烘干衣物,现在有n个衣物需要烘干,每件衣服都有一个值表示含水量,烘干机一秒可以烘干k滴水,一件衣服不在烘干机上时会每秒自动蒸发一滴水,求最少用多少时间烘干所有衣服. 分析: ...
- redis学习(六)主从复制
主从复制:主机更新数据后,根据配置和策略,自动同步到备机的master/slaver机制,master以写为主,slaver以读为主.redis支持主从复制. 下面通过实例来讲解主从复制的四个模式: ...
- Python之面向对象:属性
一.属性定义 1.类属性 类属性定义在类中且在函数体之外:类属性通常不作为实例属性使用:类变量紧接在类名后面定义 类属性的引用:类名.count eg:Employee.count 实例中可以引用类的 ...
- 品酒大会(uoj 131)
一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品酒家”和“首席猎手”两个奖项,吸引了众多品酒师参加. 在大会的晚餐上,调酒师 Rainbow 调制了 ...
- 洛谷 P1038 神经网络
题目背景 人工神经网络(Artificial Neural Network)是一种新兴的具有自我学习能力的计算系统,在模式识别.函数逼近及贷款风险评估等诸多领域有广泛的应用.对神经网络的研究一直是当今 ...
- c#.net用JavaScript实现 时钟显示
原文发布时间为:2008-10-10 -- 来源于本人的百度文章 [由搬家工具导入] 显示日期,星期,以及时间: JS文件夹与default.aspx在同一个文件夹下 JS文件夹中有date.js文件 ...
- linux下编译安装gcc5.1
一.下载源代码:ftp://ftp.mirrorservice.org/sites/sourceware.org/pub/gcc/releases/gcc-5.1.0/gcc-5.1.0.tar.bz ...