C++ for循環


C++11的for設計有個小缺陷,對range的begin和end要求為相同的類型。

例如:std::vector<int> vec;  vec.begin() 和 vec.end()是相同類型的。

例如:{1,2,3,4,5,6} 被編譯器生成std::initialize_list<int>容器,也具有相同類型的begin()和end()  注:迭代器類型可簡單的理解為int*

因此:for ( int i : {1,2,3,4,5,6} ) 

                do_someting();

被編譯器展開的偽代碼為:

1 {
2    auto&& __range = {1,2,3,4,5,6} ; 
3    for (auto __begin = __range.begin() , __end = __range.end(); 
4        __begin != __end; ++__begin) 
5    { 
6        i = *__begin; 
7        do_something();
8    }
9 } 

我們知道,int a=1, b=1.0f ;  b的類型也是int, 而不是double

同理 auto a=1, b=1.0; 也具有相同的行為,b的類型同第一個變量a的類型。

C++11對for的處理,略顯不足。因為只要求__end與__begin能比較不同即可,而不需要強制為相同的類型。

C++17 對for做了改進。

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

例子:

#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"
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM