C++11 你真的会用迭代器(iterator)么?

C++ STL提供了丰富的标准容器(Container)对象(vector,array,queue,list,set,unordered_map/set…),让我们可以根据需求选择不同的容器管理各种类型的数据。说到使用容器,不用迭代器(iterator)是不可能的,所有的容器对象都根据容器的特点都提供了类似但不同的iterator,用于访问容器中的数据。

迭代器(iterator)循环

一般来说,如果要遍历一个容器中的所有数据,程序员们最常用的写法是:

#include <list>
#include <iostream>
int main(){
list<int> lst;
for(list<int>::iterator itor=lst.begin();itor!=lst.end();itor++){
	cout<<(*itor)<<endl;
	//do something
}
}

基于范围的for循环

C++11提供了关于for循环的新特性:基于范围的for循环( the range-base for statement),再加上"类型推导"特性可以将上面的代码进一步简化:

for(auto &node:lst){
	cout<<node<<endl;
	//do something
}

没有区别吗?

显然,新的for循环写法更简洁,但新的for循环写法的优点仅此而已吗?
仔细琢磨,你会注意到,第一种写法,每次循环,都要调用lst.end(),

这是list.end()函数的源代码(来自C++11中头文件stl_list.h):

      /**
       *  Returns a read/write iterator that points one past the last
       *  element in the %list.  Iteration is done in ordinary element
       *  order.
       */
      iterator
      end() _GLIBCXX_NOEXCEPT
      { return iterator(&this->_M_impl._M_node); }

可以看出,每一次调用end()函数,都会返回一个iterator对象,根据迭代器的特性我们可以知道在整个迭代循环过程中,每次调用end()返回的对象其实都是完全一样的,而每次的调用都不可避免会发生对象构造、复制。。。等等动作,这对于要求高性能的应用场合,这种无意义的重复是不可接受的。

那么基于范围的for循环( the range-base for statement)会不会是同样的处理方式呢?
为了验证这个问题,我做了一个试验:
在我的上一篇文章
《C++11 为自定义容器实现标准的forward迭代器》中我实现了一个基于自定义哈希表(HashTableAbstract)的标准forward迭代器。于是我在HashTableAbstract 的end()函数中加入了调试代码,这样每次end()都会输出调试信息:

	iterator end()noexcept
	//{ return iterator(this->m_table,this->m_table.capacity); }//原代码
	{
		cout << "return a end iterator" << endl;//输出调试信息
		return iterator(this->m_table, this->m_table.capacity);
	}

然后运行如下测试代码:

	HashSetCl<int> testhash;//HashSetCl是基于HashTableAbstract子类,实现哈希集合
	testhash.insert(2000);//加入3条数据
	testhash.insert(65535);
	testhash.insert(12345);
	cout<<"testing for statement using iterator:"<<endl;//使用迭代器循环
	for (auto itor = testhash.begin(); itor != testhash.end(); itor++) {
		cout << *itor << endl;
	}
	cout<<"testing for the range-base for statement:"<<endl;//使用基于范围的for循环
	for (auto n : testhash) {
		cout << n << endl;
	}

以下是程序输出(debug/release结果一样)

testing for statement using iterator://注,循环中调用了三次end()
return a end iterator
2000
return a end iterator
12345
return a end iterator
65535
return a end iterator
testing for the range-base for statement://注,循环中调用了一次end()
return a end iterator
2000
12345
65535

总结

上面的输出可以证实,基于范围的for循环( the range-base for statement)只会在循环开始的时候调用一次end(),与一般直接使用迭代器(iterator)的循环相比,不仅具备代码更简洁的优点,性能上也更具备优势。当然这个结论只在无序容器迭代遍历(只读)的情况下才有效(无序容器只提供forward迭代器),具备随机访问迭代器(random-access iterator)的容器(比如 vector,array),直接用下标访问才是最快的。

如果你还是"坚持传统",习惯直接使用迭代器来工作,那么建议对代码做一些改进,还以最前面的代码为例,在循环开始时调用一次end()函数保存成临时变量end,然后每次循环比较的时候不再调用end()函数,而是直接与临时变量end相比,就避免了重复调用end()

for(auto itor=lst.begin(),end=lst.end();itor!=end;itor++){
	cout<<(*itor)<<endl;
	//do something
}
  • 7
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

10km

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值