C++式迭代.

C++式迭代.
for(std::string::const_iterator it = str.begin(); it != str.end(); ++it)
        // ........
 
上面的it就是迭代器, 就可以理解为以下的样子
char c_str[100] = "xxx";
for(char* it = c_str; *it != '\0'; ++it)
        // .......
 
上面是C语言式的迭代, it也是迭代器..
 
概念上来讲, 迭代器可以使指针, 也可以是任何类型, 只要可以用来遍历容器(数组)就可以了.
 
用在C++泛型算法里面比较多.  比如像下面的泛型函数copy.
就是接受任何有"迭代器"概念的类型作为参数, 可以使指针, 也可以是其他类型.
 
template <typename _In, typename _Out>
        _Out copy(_Out _Dest, _In _First, _In _Last);


    for (vector<SegmentSet>::iterator it = vecSegset.begin(); it != vecSegset.end(); it++) {
        //TODO: we have to release manually here to avoid memory leak.
        //  Other alternative ways are passing only pointers rather than the objects themselves,
        //  or adopting reference count to release automatically.
        it->release();
    }

ArrayList< String > arr = new ArrayList< String >();Iterator it = arr.iterator();迭代的时候可以这样while( it .hasNext() ){ //做一些处理,比如 System.out.print( it.next );}配合上泛型,一个好处是it.next()可以不用类型转换
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值