C/C++ STL

之前对C++ STL一点不懂。在看list时,看到list中insert方法返回值为迭代器。

在看迭代器中,了解到迭代器可被用来访问一个容器类的所包函的全部元素。
</pre><pre class="cpp" name="code">如下代码对vector容器对象生成和使用了迭代器:
#include<iostream>
#include<vector>
using namespace std;
vector<int> the_vector;
vector<int>::iterator the_iterator;
int main(){
	
	for(int i=0;i<10;i++)
		the_vector.push_back(i);//push_back()添加值为i的元素到当前vector末尾
	int total=0;
	the_iterator=the_vector.begin();//begin()函数返回一个指向当前vector起始元素的<a target=_blank href="mk:@MSITStore:D:\用户目录\Documents\Tencent%20Files\1508108373\FileRecv\C语言程序设计\C语言库函数参考.chm::/cppreference.com/iterators.html">迭代器</a>.
	while(the_iterator!=the_vector.end()){   //end() 函数返回一个指向当前vector末尾元素的<strong>下一位置</strong>的<a target=_blank href="mk:@MSITStore:D:\用户目录\Documents\Tencent%20Files\1508108373\FileRecv\C语言程序设计\C语言库函数参考.chm::/cppreference.com/iterators.html">迭代器</a>.
		total+=*the_iterator;
		the_iterator++;			//++操作符用来递增迭代器,以访问容器中的下个对象。
	}
	cout<<"Total="<<total<<endl;
	return 0;
	
}

算不上完全自己做的。但是也是个进步吧。
参考:<a target=_blank href="http://blog.chinaunix.net/uid-20773165-id-1847758.html">http://blog.chinaunix.net/uid-20773165-id-1847758.html</a>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值