Skip to content

maisonsmd/cpp-ranged-iterator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 

Repository files navigation

Motive

I like this type of code in python

# loop from 0 to 10
for i in range(10):
    print(i)

# loop from 5 to 10 with step=2
for i in range(5, 20, 2):
    print(i)

Meanwhile in C++

// loop from 5 to 10
for (int i = 0; i < 10; i++)
    std::cout << i;

// loop from 5 to 10 with step=2
for (int i = 5; i < 10; i+=2)
    std::cout << i;

Nah, it looks like legacy C to me, why be so ugly?

Now we can do this instead

// loop from 0 to 10
for (auto i : range(10))
    std::cout << i;

// loop from 0 to 10 with step=2
for (auto i : range(10).step(2))
    std::cout << i;

// loop from 5 to 10
for (auto i : range(5, 10))
    std::cout << i;

// loop from 5 to 10 with step=2
for (auto i : range(5, 10, 2))
    std::cout << i;

Check out main.cpp for more examples

Overhead?

I don't think so, in this benchmark, RangedIterator is even faster than a normal for-loop (optimization flag set to -O1, you won't build your code without any optimization, will you?)

Anyway it's just sugar syntax, most compilers will opimize it away.

About

Python-like iterator for modern C++ for-loops

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages