使用基于范围的for循环在子范围上进行迭代 [英] using range based for loop for iterating on a sub range

查看:57
本文介绍了使用基于范围的for循环在子范围上进行迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用基于范围的for循环遍历子范围?

Is it possible to loop over sub range using range based for loop ?

std::vector <std::string> inputs={"1","abaaaa","abc","cda"};

for (auto &it : new_vector(inputs.begin()+1, inputs.end()))
{
....
}

推荐答案

您可以使用Boost的 iterator_range :

You could use Boost's iterator_range:

for (auto &it : boost::make_iterator_range(inputs.begin()+1, inputs.end()))
{
    cout << it << endl;
}

演示

或者,您可以编写自己的包装器.

Alternatively you could write your own wrapper.

这篇关于使用基于范围的for循环在子范围上进行迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆