C++17尝鲜:基于区间的for循环

C++11首次引入了基于区间(范围)的for循环
range-for

for ( range_declaration : range_expression ) loop_statement

其定义如下

{
   auto && __range = range_expression ; 
   for (auto __begin = begin_expr, __end = end_expr; 
       __begin != __end; ++__begin) { 
       range_declaration = *__begin; 
       loop_statement 
   }
} 

和传统for循环不同,这种基于区间的for循环可以在区间的元素上直接迭代:

std::vector<int> v = {1, 2, 3, 4};
for (int i : v)
    std::cout << i << " ";

按照定义,编译器内部会把这段代码扩展成:

std::vector<int> v = {1, 2, 3, 4};
{
   auto && __range = v ; 
   for (auto __begin = v.begin(), __end = v.end(); 
       __begin != __end; ++__begin) { 
       int i = *__begin; 
       std::cout << i << " ";
   }
}

C++17将其定义修改为

{        
    auto && __range = range_expression ; 
    auto __begin = begin_expr ;
    auto __end = end_expr ;
    for ( ; __begin != __end; ++__begin) { 
        range_declaration = *__begin; 
        loop_statement 
    } 
}

从而引来了如下问题:
How the new range-based for loop in C++17 helps Ranges TS?

#include <iostream>
#include <string>

// a struct to get the first word of a string
struct FirstWord {
    std::string data;

    // declare a predicate to make ' ' a string ender
    struct EndOfString {
        bool operator()(std::string::iterator it) { return (*it) != '\0' && (*it) != ' '; }
    };

    std::string::iterator begin() { return data.begin(); }
    EndOfString end() { return EndOfString(); }
};

// declare the comparison operator
bool operator!=(std::string::iterator it, FirstWord::EndOfString p) { return p(it); }

// test
int main() {
    for (auto c : {"Hello World !!!"})
        std::cout << c;
    std::cout << std::endl; // print "Hello World !!!"

    for (auto c : FirstWord{"Hello World !!!"}) // works with gcc with C++17 enabled
        std::cout << c;
    std::cout << std::endl; // print "Hello"
}


  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值