Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create my own loop version in C++?

Tags:

c++

I was wondering if it possible to create custom functions like for, for_each, while etc.

There's nothing that I want to do that the existing loops won't do it. I am just curious to learn how they work and if I ever need to create my own.

For example if one wants to create another version of the for function that would take only parameter.

In this example, I want to to create a for that only takes one parameter, an integer. Instead of writing

for (int i = 0; i < 50; ++i)

I would create a for version like this

for_(50)

and they would act the same. How would I do something like that?

I have posted this question in another forum.

like image 954
HBatalha Avatar asked Dec 09 '19 16:12

HBatalha


People also ask

What is for loop in C with example?

Example 2: for loop The count is initialized to 1 and the test expression is evaluated. Since the test expression count<=num (1 less than or equal to 10) is true, the body of for loop is executed and the value of sum will equal to 1. Then, the update statement ++count is executed and count will equal to 2.

What is do while loop in C?

ANSWER. The C do while statement creates a structured loop that executes as long as a specified condition is true at the end of each pass through the loop.

How many loops are there in C++ 98?

9. How many types of loops are there in C++? Explanation: There are four types of loop.


Video Answer


4 Answers

In addition to the proposals in other answers, you could create a function like the one below, but it is, at the very end, very similar to using the standard std::for_each.

#include <iostream>
#include <functional>

template<typename C, typename F>
void for_(C begin_, C end_, F&& f) { // [begin_, end_)
    for (C i =  begin_; i < end_; ++i) {
        f(i);
    }
}

template<typename C, typename F>
void for_(C count, F&& f) { // special case for [0, count)
    for_(0, count, f);
}

void mul2(int x) {
    std::cout << x*2 << " ";
}

int main() {
    for_(10, [](int i) { std::cout << i << "\n"; });
    for_(2, 10, mul2);
}
like image 76
cbuchart Avatar answered Oct 15 '22 00:10

cbuchart


An ugly and unsafe solution is to use macro:

#define REPEAT(i,N) for(int (i) = 0; (i) < (N); ++(i))

int main()
{
  REPEAT(i,10) std::cout << i << std::endl;

  return 0;
}
like image 23
Picaud Vincent Avatar answered Oct 15 '22 01:10

Picaud Vincent


You can't extend the C++ syntax for new loops.

You could use a macro, but this is pretty ugly, and generally best avoided. Another way to get something similar is by passing a functor as a parameter, greatly helped by the introduction of lambda expressions to C++. You can find some examples of such in the <algorithm> header.

For example:

#include <algorithm>
#include <vector>
int main()
{
    std::vector<int> numbers = { 1, 4, 5, 7, 10 };
    int even_count = 0;
    for (auto x : numbers)
    {
        if (x % 2 == 0)
        {
            ++even_count;
        }
    }
    auto even_count2 = std::count_if(numbers.begin(), numbers.end(), [](int x) { return x % 2 == 0; });
}
like image 27
Fire Lancer Avatar answered Oct 15 '22 00:10

Fire Lancer


You could use a lambda function and pass in a function object as a parameter to be performed for every iteration of the loop.

#include <iostream>
#include <functional>

int main()
{
    auto for_ = [](int start, int size, std::function<void (int i)> fn)
    {
        int end = start + size;

        for (int i = start; i < end; ++i)
        {
            fn(i);
        }
    };

    for_(0, 10, [](int i) { std::cout << i << std::endl; });
    for_(0, 10, [](int i) { std::cout << i*2 << std::endl; });
}

It seems like you are reinventing the wheel here a bit. You could just use std::for_each.

However, you could have custom lambda functions that do different things and just implement the operation within the lambda itself without taking in a function object for the operation.

like image 31
jignatius Avatar answered Oct 14 '22 23:10

jignatius