DEV Community

Discussion on: Let's try C++20 | Range-based for statements with initializer

Collapse
 
sandordargo profile image
Sandor Dargo

That's a cool feature, even the if with initializer. By the way, if you have access to boost, you have one more option that works before C++20:

#include <iostream>
#include <array>
#include <string>
#include <boost/range/adaptor/indexed.hpp>


int main() {
    std::array<std::string, 3> data = {"hello", ",", "world"};

    using namespace boost::adaptors;
    for (auto const& d : data | indexed(0))
    {
        std::cout << (d.index() + 1) << " - " << d.value() << '\n';
    }

}
Enter fullscreen mode Exit fullscreen mode